javax.ws.rs.ProcessingException: 找不到内容类型 application/x-www-form-urlencoded 类型的作者

javax.ws.rs.ProcessingException: could not find writer for content-type application/x-www-form-urlencoded type

我正在使用 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>     

Connection is working fine and sending correct response while requesting using POSTMAN

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

回复:

javax.ws.rs.ProcessingException: could not find writer for content-type application/x-www-form-urlencoded type

请帮忙...

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

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

您也可以使用 Entity.form(connectionRequest),即 shorthand,这样您就不必使用 MediaType.APPLICATION_FORM_URLENCODED_TYPE


顺便说一句,请参阅 以解析响应。你不需要依赖。您已经有了用于 RESTEasy 的那个。

我创建了一组 reader/writers 你可以导入它来处理表单编码到 java 对象的自动绑定:https://github.com/exabrial/form-binding 这比创建表单实例要容易一些。