我正在 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 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>
连接工作正常,同时发送正确的响应 请求使用POSTMAN
但在请求使用该程序后,它会将错误创建为
回应:
javax.ws.rs.ProcessingException:找不到编写器 content-type application / x-www-form-urlencoded type
请帮忙......
答案 0 :(得分:3)
您无法使用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的那个。
答案 1 :(得分:0)
我创建了一组可以导入的读取器/写入器,它们可以处理将表单编码自动绑定到java对象:https://github.com/exabrial/form-binding比创建Form实例要容易一些。