Reputation: 3461
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...
Upvotes: 3
Views: 9570
Reputation: 8837
I created a set of reader/writers you can import that handle automatic binding of form encoding to java objects: https://github.com/exabrial/form-binding it's a little easier than creating an instance of a Form.
Upvotes: 0
Reputation: 209002
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.
Upvotes: 10