javax.ws.rs.ProcessingException:找不到内容类型应用程序/x-www-form-urlencoded 类型的编写器

Chi*_*asa 1 java web-services jax-rs resteasy maven

我正在使用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());
Run Code Online (Sandbox Code Playgroud)

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>     
Run Code Online (Sandbox Code Playgroud)

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

在此处输入图片说明

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

回复 :

javax.ws.rs.ProcessingException:找不到内容类型应用程序/x-www-form-urlencoded 类型的编写器

请帮忙...

Pau*_*tha 5

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

Form connectRequest = new Form()
    .param("username", "...")
    .param("password", "...")
    .param("client_id")
    ...;
Run Code Online (Sandbox Code Playgroud)

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


顺便说一句,请参阅此内容以解析响应。你不需要依赖。您已经拥有了 RESTEasy。