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?
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?
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.
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
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With