javax.ws.rs.ProcessingException:找不到内容类型应用程序/x-www-form-urlencoded 类型的编写器 [英] javax.ws.rs.ProcessingException: could not find writer for content-type application/x-www-form-urlencoded type

查看:51
本文介绍了javax.ws.rs.ProcessingException:找不到内容类型应用程序/x-www-form-urlencoded 类型的编写器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 MediaType.APPLICATION_FORM_URLENCODED_TYPE 中的resteasy-client"库发送 POST 请求.

示例代码:

String serviceUrl = "URL";ConnectRequest connectRequest = new ConnectRequest();connectRequest.setUsername("");connectRequest.setPassword("");connectRequest.setScope("承载");connectRequest.setGrant_type("");实体<连接请求>实体 = Entity.entity(connectRequest,MediaType.APPLICATION_FORM_URLENCODED_TYPE);ResteasyClient 客户端 = new ResteasyClientBuilder().build();ResteasyWebTarget target = client.target(serviceUrl);响应 response = target.request().post(entity);System.out.println("RESP : "+response.toString());

Maven 依赖

 <java.version>1.7</java.version><resteasy.version>3.0.4.Final</resteasy.version></属性><依赖项><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></依赖><依赖><groupId>org.jboss.resteasy</groupId><artifactId>resteasy-client</artifactId><version>${resteasy.version}</version></依赖><依赖><groupId>org.jboss.resteasy</groupId><artifactId>resteasy-jackson-provider</artifactId><version>${resteasy.version}</version></依赖><依赖><groupId>org.jboss.resteasy</groupId><artifactId>resteasy-servlet-initializer</artifactId><version>${resteasy.version}</version></依赖><依赖><groupId>org.jboss.resteasy</groupId><artifactId>resteasy-jaxrs</artifactId><version>${resteasy.version}</version></依赖><依赖><groupId>org.jboss.resteasy</groupId><artifactId>jaxrs-api</artifactId><version>${resteasy.version}</version></依赖><依赖><groupId>org.jboss.resteasy</groupId><artifactId>resteasy-jaxb-provider</artifactId><version>${resteasy.version}</version></依赖><依赖><groupId>org.jboss.resteasy</groupId><artifactId>resteasy-multipart-provider</artifactId><version>${resteasy.version}</version></依赖></依赖项>

<块引用>

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

但是在请求使用该程序后,它会创建错误为

回复:

<块引用>

javax.ws.rs.ProcessingException:找不到作者内容类型应用程序/x-www-form-urlencoded 类型

请帮忙...

解决方案

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

Form connectRequest = new Form().param("用户名", "...").param("密码", "...").param("client_id")...;

您也可以使用 Entity.form(connectionRequest),它是速记,因此您不必使用 MediaType.APPLICATION_FORM_URLENCODED_TYPE.

<小时>

顺便说一句,请参阅这也以解析响应.你不需要依赖.您已经拥有了 RESTEasy.

I'm sending POST request using "resteasy-client" library in MediaType.APPLICATION_FORM_URLENCODED_TYPE.

Sample Code :

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 Dependencies

    <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

But after requesting using the program it creates error as

Response :

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

Please help...

解决方案

You can't use a POJO to send application/x-www-form-urlencoded. You need to use the javax.ws.rs.core.Form class.

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

You can also use Entity.form(connectionRequest), which is shorthand so you don't have to use the MediaType.APPLICATION_FORM_URLENCODED_TYPE.


As an aside, see this also for parsing the response. You won't need the dependency. You already have the one for RESTEasy.

这篇关于javax.ws.rs.ProcessingException:找不到内容类型应用程序/x-www-form-urlencoded 类型的编写器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆