我在 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:找不到内容类型application / x-www-form-urlencoded类型的writer
请帮忙...
1 回答
您不能使用POJO发送
application/x-www-form-urlencoded
. 您需要使用javax.ws.rs.core.Form类 .您也可以使用
Entity.form(connectionRequest)
,这是速记,因此您不必使用MediaType.APPLICATION_FORM_URLENCODED_TYPE
.另外,请参阅this also以解析响应 . 你不需要依赖 . 你已经有了RESTEasy的那个 .