Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAX-RS response.getEntity returns null after post

Tags:

java

post

jax-rs

When i invoke next code:

Response response = target.request(MediaType.APPLICATION_JSON_TYPE)
                .post(Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE));

response.getEntity();

response.getEntity() is always null.

But when i invoke:

JsonObject json = target.request(MediaType.APPLICATION_JSON_TYPE)
                .post(Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE), JsonObject.class);

variable json is not null.

I need to use first variant because i need to check response status.

Why the first code is not working?And how can i get status code then?

like image 536
user3245504 Avatar asked Aug 19 '15 19:08

user3245504


People also ask

What is JAX-RS REST API?

JAX-RS 2.0 (JSR-339) and JAX-RS 2.1 (JSR-370), are JCP (Java Community Process) specifications that provide a Java API for RESTful Web Services over the HTTP protocol. Some of the more well known JAX-RS API implementations are RESTEasy and Jersey. JAX-RS has annotations for responding to HTTP requests. What is RESTEasy?

What are the JAX-RS annotations used for?

And the framework makes good use of JAX-RS annotations to simplify the development and deployment of these APIs. JAX-RS 2.0 (JSR-339) and JAX-RS 2.1 (JSR-370), are JCP (Java Community Process) specifications that provide a Java API for RESTful Web Services over the HTTP protocol.

How do I return plain text as a Jersey response?

The endpoint shown here is a simple example of how plain text can be returned as a Jersey response: We can do an HTTP GET using curl to verify the response: This endpoint will send back a response as follows: When the media type isn't specified, Jersey will default to text/plain. 3.2. Error Response

How to deploy resteasy web application on Tomcat server?

In this tutorial, we use the tomcat server to deploy the RESTEasy web application. 1. Create a Maven Web project in Eclipse IDE 2. Add maven dependencies Here is the complete Maven pom.xml file.


1 Answers

You need to use response.readEntity(Your.class) to return the instance of the type you want. For example

String rawJson = response.readEntity(String.class);
// or
JsonObject jsonObject = response.readEntity(JsonObject.class);

Note that there actually needs to be a provider to handle reading that Java type and application/json. If you are using the Jersey and the JSON-P API, see this. Also for general information about providers, see this

like image 111
Paul Samsotha Avatar answered Oct 19 '22 10:10

Paul Samsotha