【问题标题】:javax.ws.rs.ProcessingException: could not find writer for content-type application/x-www-form-urlencoded typejavax.ws.rs.ProcessingException:找不到内容类型应用程序/x-www-form-urlencoded 类型的编写器
【发布时间】:2018-02-02 13:44:47
【问题描述】:

我正在使用 MediaType.APPLICATION_FORM_URLENCODED_TYPE 中的“resteasy-client”库发送 POST 请求。

示例代码:

String serviceUrl = "URL";

    ConnectRequest connectRequest = new ConnectRequest();
    connectRequest.setUsername("");
    connectRequest.setPassword("");
    connectRequest.setScope("bearer");
    connectRequest.setGrant_type("");

    Entity<ConnectRequest> entity = Entity.entity(connectRequest,
                MediaType.APPLICATION_FORM_URLENCODED_TYPE);

    ResteasyClient client = new ResteasyClientBuilder().build();
    ResteasyWebTarget target = client.target(serviceUrl);

    Response response = target.request().post(entity);

    System.out.println("RESP : "+response.toString());

Maven 依赖项

    <properties>
    <java.version>1.7</java.version>
    <resteasy.version>3.0.4.Final</resteasy.version>
</properties>
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-client</artifactId>
            <version>${resteasy.version}</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jackson-provider</artifactId>
            <version>${resteasy.version}</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-servlet-initializer</artifactId>
            <version>${resteasy.version}</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jaxrs</artifactId>
            <version>${resteasy.version}</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>jaxrs-api</artifactId>
            <version>${resteasy.version}</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jaxb-provider</artifactId>
            <version>${resteasy.version}</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-multipart-provider</artifactId>
            <version>${resteasy.version}</version>
        </dependency>
</dependencies>     

连接工作正常并发送正确的响应,同时 使用 POSTMAN 请求

但是在请求使用该程序后,它会产生错误

回应:

javax.ws.rs.ProcessingException:找不到编写器 content-type application/x-www-form-urlencoded 类型

请帮忙...

【问题讨论】:

    标签: java maven web-services jax-rs resteasy


    【解决方案1】:

    您不能使用 POJO 发送 application/x-www-form-urlencoded。您需要使用javax.ws.rs.core.Form 类。

    Form connectRequest = new Form()
        .param("username", "...")
        .param("password", "...")
        .param("client_id")
        ...;
    

    您也可以使用Entity.form(connectionRequest),这是一种简写形式,因此您不必使用MediaType.APPLICATION_FORM_URLENCODED_TYPE


    顺便说一句,请参阅 this also 以解析响应。您将不需要依赖项。你已经有了 RESTEasy 的。

    【讨论】:

      【解决方案2】:

      我创建了一组您可以导入的读取器/写入器,它们处理表单编码与 java 对象的自动绑定:https://github.com/exabrial/form-binding 这比创建表单实例要容易一些。

      【讨论】:

        猜你喜欢
        • 2018-04-15
        • 1970-01-01
        • 2021-02-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-02
        • 1970-01-01
        相关资源
        最近更新 更多