RESTEasy: #Form deserialization issue in a #POST method - json

Here's a puzzle!
In a simple POST implementation:
#POST
#Consumes(MediaType.APPLICATION_JSON)
public Response promote(#Form PromotionForm promotion) {
return Response.status(Response.Status.OK)
.entity(promotion.toString())
.build();
}
the argument passed to me does not have values set:
PromotionForm{name='null', csid=null}
But in debugger I can see that the request server received has the values in its input stream:
new BufferedReader(new InputStreamReader(
((HttpServletInputMessage) request).getInputStream())).readLine()
// returns: {"name":"form","csid":123}
After some debugging I could see that RESTEasy tries to derive arguments for the POST method call:
args[i++] = extractor.inject(input, response);
Which leads to FormInjector code:
propertyInjector.inject(request, response, target);
And eventually to FormParamInjector:
List<String> list = request.getDecodedFormParameters().get(paramName);
But request.getDecodedFormParameters() size is 0. RESTeasy does not try to read
anything from the requests' input stream for some reason.
Any ideas how I can make RESTeasy populate PromotionForm object correctly?
More information below.
Thanks for all you answers and comments in advance.
The client call is:
final PromotionForm form = new PromotionForm();
form.setName("form");
form.setCsid(123L);
final Response response = new ResteasyClientBuilder()
.disableTrustManager()
.build()
.target(targetField.getValue())
.request(requestField.getValue())
.cookie(cookieNameField.getValue(), cookieValueField.getValue())
.buildPost(Entity.entity(form, MediaType.APPLICATION_JSON_TYPE))
.invoke();
The PromotionForm:
import javax.ws.rs.FormParam;
public class PromotionForm {
#FormParam("name")
private String name;
#FormParam("csid")
private Long csid;
// setters & getters omitted
Dependencies:
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.0.4.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson2-provider</artifactId>
<version>3.0.4.Final</version>
</dependency>
<!-- scannotation & resteasy-client ommitted -->
web.xml snippet:
<context-param>
<param-name>resteasy.scan</param-name>
<param-value>true</param-value>
</context-param>
<servlet>
<servlet-name>resteasy-servlet</servlet-name>
<servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>resteasy-servlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<context-param>
<!--If the url-pattern for the Resteasy servlet-mapping is not /*-->
<param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>/rest</param-value>
</context-param>

Your method says that it consumes application/json. But the PromotionForm-class has #FormParam-annotations, which as the name implies, consumes form data.
To fix this, do one of the following:
Try to post form data instead of json. And change your #Consumes-annotation value to application/x-www-form-urlencoded
OR
Remove the #Form-annotation. Add JAXB-annotations on PromotionForm, so that the json data you post can be mapped to the PromotionForm-class.

Related

Spring Jersey return proxy objects in JSON

I'm using Spring Data Repository to persistence objects. I'm trying to return them by REST web api (Jersey) and getting an error, but when i'm trying to return normal POJO object, all looks fine, Jersey parse object to JSON and i'm getting response 200 code.
#POST
#Path("/test")
#Produces( MediaType.APPLICATION_JSON )
public Document test() {
Document d = documentRepository.findOne(123L);
return d; // response code 500 without any stack trace
return new Document(); // normal JSON object in response content
}
My dependencies for jersey:
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-spring3</artifactId>
<version>2.16</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-processing</artifactId>
<version>2.16</version>
</dependency>
web.xml:
<servlet>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
How can i return proxy object from spring data repository, or List of these objects ?

How to receive JSON Messages in POST body in a JAX-RS Restful web service in CXF?

I'm trying to develop a REST service using Apache-CXF, on top of JAX-RS. For starters, I have a method called test that receives a String message and int value. I want the clients to be able to pass these parameters in a POST message body. I can't seem to achieve this.
Before I paste the code here, here are some details:
I'm using CXF without Spring
It's not a web app, so I don't have the WEB-INF folder with the web.xml
I test the service using SoapUI and Postman (Google Chrome application)
With the following code, I get WARNING: javax.ws.rs.BadRequestException: HTTP 400 Bad Request:
DemoService.java
#WebService(targetNamespace = "http://demoservice.com")
#Path("/demoService")
public interface DemoService {
#POST
#Path("/test")
#Produces(MediaType.APPLICATION_JSON)
#Consumes(MediaType.APPLICATION_JSON)
public String test (String message, int value);
}
DemoServiceImpl.java
public class DemoServiceImpl implements DemoService {
#Override
public String test(String message, int value) {
return "test message: " + message + " value = : " + value;
}
}
DemoServer.java
public class DemoServer{
public static void main(String[] args) {
JAXRSServerFactoryBean serverFactory = new JAXRSServerFactoryBean();
DemoService demoService = new DemoServiceImpl();
serverFactory.setServiceBean(demoService);
serverFactory.setAddress("http://localhost:9090");
serverFactory.create();
}
}
My POM.xml (minus the attributes in the root tag, everything's there)
<project ...>
<modelVersion>4.0.0</modelVersion>
<groupId>demo</groupId>
<artifactId>demoService</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<cxf.version>3.0.0</cxf.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>${cxf.version}</version>
</dependency>
<!-- Jetty is needed if you're are not using the CXFServlet -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-service-description</artifactId>
<version>3.0.0-milestone1</version>
</dependency>
</dependencies>
</project>
Testing with {"message":"hello there!", "value":"50"} to the URL http://localhost:9090/demoService/test gave a HTTP 400 Bad Reuest.
Then I saw this question on S.O.: How to access parameters in a RESTful POST method and tried this:
added the following nested class in DemoServer.java:
#XmlRootElement
public static class TestRequest {
private String message;
private int value;
public String getMessage() { return message; }
public void setMessage(String message) { this.message = message; }
public int getValue() { return value; }
public void setValue(int value) { this.value = value; }
}
I also modified the DemoService interface and the implementation to use this class as a parameter in the test method, although this is still ultimately not what I want to do. (just showing the implementation here, question's already getting long):
#Override
public String test(TestRequest testRequest) {
String message = testRequest.getMessage();
int value = testRequest.getValue();
return "test message: " + message + " value = : " + value;
}
And to fix this error that I got: SEVERE: No message body reader has been found for class DemoService$TestRequest, ContentType: application/json (in Postman I see error 415 - unsupported media type) I added the following dependencies (jettison and another thing) to the POM.xml:
<dependency>
<groupId>org.codehaus.jettison</groupId>
<artifactId>jettison</artifactId>
<version>1.3.5</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-extension-providers</artifactId>
<version>2.6.0</version>
</dependency>
I tested the service using the following JSON message, in a HTTP POST request:
{"testRequest":{"message":"hello there!", "value":"50"}}
This works. Though this solution where I use a TestRequest class to encapsulate the parameters works, that's not the solution I'm looking for. I want to be able to pass the two parameters in a JSON message, without having to introduce this TestRequest class (explicitly).
Questions:
Would this be easier to implement using Jersey?
I don't have a web.xml nor a WEB-INF folder, so I can't configure CXF in a cxf.xml file can I? A lot of tutorials online seem ot use a lot of XML configuration, but I don't want to deploy a framework like TomEE or Spring or Glassfish just to do that.
Searching online for solutions, I came across Spring Boot. Would you recommend using that, perhaps? Would that make developing web services like this easier?
Also, how do I get it to return the value in JSON format (or is it not supposed to do that for Strings?)
My friend pointed me to this stack exchange question: JAX-RS Post multiple objects
and also the following documentation: http://cxf.apache.org/docs/jax-rs-and-jax-ws.html
which states:
public class CustomerService {
public void doIt(String a, String b) {...};
}
By default JAX-RS may not be able to handle such methods as it
requires that only a single parameter can be available in a signature
that is not annotated by one of the JAX-RS annotations like
#PathParam. So if a 'String a' parameter can be mapped to a #Path
template variable or one of the query segments then this signature
won't need to be changed :
#Path("/customers/{a}")
public class CustomerService {
public void doIt(#PathParam("a") String a, String b) {...};
}
So, to answer my question, NO, it cannot be done.

Jersey 1.17.1 POJO Mapping Feature Server and how to get from client?

I am running a REST service on server and I want to convert my List of POJO into Json. I don't want to use #XMLRootElement JA-RX because it is only good for XML. If you Google you will find that Jackson is very good choice for Json.
Is there anyone who have solved this problem and please paste complete Server and Client Code?
Note:
I spent 16 hours in just finding out how to do this and when I replied on questions they deleted my answer so I decided to put this here to save others valueable time and I believe in Knowledge sharing.. Please if you can improve my code. I am always open to suggestions.
Detailed Reply includes Server and Client sample implementation with
JSON Marshalling and Unmarshalling
Note: Json POJO Mapping features is done using Jackson
I spent a whole day in finidng why message body write was not found. What I was doing wrong is I was using JAXB javax.xml.bind.annotation #XMLRootElement in my Jersey 1.17.1 Web Service and I was trying to unmarshall it with Jackson.
Acutally if you Google it you will find that JAXB is only good for XML but for JSON Jackson is excellent. I also forgot to put some configuration paramters in my web.xml that enable POJO Mapping feature.
Here is the snap of how you your servlet mapping should be to enable POJO mapping feature of Jackson.
<!-- WebService -->
<servlet>
<servlet-name>REST Service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
<param-value>com.sun.jersey.api.container.filter.LoggingFilter;com.algo.server.webservice.WebServiceRequestFilter</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.algo.server.webservice;org.codehaus.jackson.jaxrs</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
You also need to add those jar files into your WEB-INF/libs folder
jackson-core-asl-1.9.2.jar
jackson-mapper-asl-1.9.2.jar
jackson-xc-1.9.2.jar
jackson-jaxrs-1.9.2.jar
This is a sample web service method that returns a list of some objects
#GET
#Produces({ MediaType.APPLICATION_JSON })
#Path("/clientId/{client_id}/clientDept/{client_department}/clientLoc/{client_location}")
public Response getTasksForClientId(#PathParam("client_id") String pClientId,
#PathParam("client_department") String pClientDepartment,
#PathParam("client_location") String pClientLocation) {
List<Task> list = new ArrayList<Task>(10);
Task task = null;
for (int i = 0; i < 10; i++) {
task = new Task();
task.setComments("These are test comments");
task.setCreatedBy(11L);
task.setCreatedOn(new Date());
task.setFromDay(new Date());
task.setFromTime(new Date());
task.setToTime(new Date());
task.setToDay(new Date());
task.setUpdatedOn(new Date());
task.setLocation("Pakistan Punajb");
task.setSpecialCoverImage("webserver\\cover\\cover001.png");
task.setTargetId(1L);
task.setTargetPlaceHolder(2);
task.setUpdatedBy(23234L);
list.add(task);
}
GenericEntity<List<Task>> entity = new GenericEntity<List<Task>>(list) {
};
return Response.ok(entity).build();
}
Client Side
Now How to use convert this JSON object on client side into same List<T> Object. It's a sinch :-)
You need to put the same class from the server that you converted into POJO. It shoulb be the same
private void checkForUpdate() {
ClientConfig clientConfig = new DefaultClientConfig();
clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
clientConfig.getClasses().add(JacksonJsonProvider.class);
Client client = Client.create(clientConfig);
WebResource webResource = client.resource("http://localhost:8080/some-server");
WebResource wr = webResource.path("rest").path("taskPublisherService").path("clientId/12").path("clientDept/2").path("clientLoc/USA");
ClientResponse clientResponse = wr.type(MediaType.APPLICATION_JSON).get(ClientResponse.class);
List<Task> lists = clientResponse.getEntity(new GenericType<List<Task>>() {});
System.out.println(lists);
}
This one from Jersey includes all the above mentioned JARs:
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>${version.jersey}</version>
<scope>runtime</scope>
</dependency>

Bad Request 400 with Spring

I get an HTTP Bad Request when trying to reach my REST API. The issue is that Spring does not give much information to debug.
The URL is http://localhost:8080/webapp/network/v1/jobs.json?callback=jQuery203040624934318475425_1377165238418&sEcho=1&iColumns=1&sColumns=&iDisplayStart=0&iDisplayLength=10&mDataProp_0=id&sSearch=&bRegex=false&sSearch_0=&bRegex_0=false&bSearchable_0=true&iSortCol_0=0&sSortDir_0=asc&iSortingCols=1&bSortable_0=true&_=1377165238419
The controller:
#Controller
#RequestMapping("/network/v1/jobs")
public class JobsController {
#RequestMapping(value = "", method = RequestMethod.GET)
public ResponseEntity<JQueryDatatablesPage<Job>> list(
#RequestParam int iDisplayStart, #RequestParam int iDisplayLength,
#RequestParam int sEcho, #RequestParam String search) {
...
}
}
In my pom file, I have jackson:
<!-- Jackson JSON Mapper -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>${jackson.version}</version>
</dependency>
The json extension is used to return JSONP:
<filter>
<filter-name>jsonpCallbackFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>jsonpCallbackFilter</filter-name>
<url-pattern>*.json</url-pattern>
</filter-mapping>
The debugger stopped in the filter but I still get a Bad Request, and the debugger does not stop in the list method.
Stupid mistake parameter search was not in the URL. Just added #RequestParam(required = false) String search)

JAX-RS JAXB JSON Response not working ( MessageBodyProviderNotFoundException)

I had been working for sometime to figure out how to create a JAX Restful Service... using the guide available here - Jersey
As explained in Section 2.3.2, I had added the below dependency in Maven -
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.0</version>
</dependency>
In web.xml
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>org.glassfish.jersey.filter.LoggingFilter</param-value>
</init-param>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.hms.rs.controller.MyApp</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
and MyApp.java
public class MyApp extends Application{
#Override
public Set<Class<?>> getClasses() {
return new HashSet<Class<?>>() {{
// Add your resources.
System.out.println("From the Myapp...");
add(Patient.class);
add(PatientController.class);
// Add LoggingFilter.
add(LoggingFilter.class);
}};
}
}
Patient.java -
#XmlRootElement(name = "Patient")
public class Patient {
private String patientFName;
private String patientLName;
private int patientAge;
private String patientSex;
private String patientParentSpouse;
private String patientQual;
private String patientOccupation;
private String patientComments;
public Patient()
{
}
Setters and Getters....
}
PatientController.java -
#Path("/ManagePatient")
public class PatientController {
#GET
#Path("/getPatient")
#Produces(MediaType.APPLICATION_XML)
public Patient printPatient() {
System.out.println("Hello.... from the PatientController");
Patient ptnt = new Patient();
ptnt.setPatientFName("FirstN");
ptnt.setPatientLName("LName");
ptnt.setPatientAge(30);
ptnt.setPatientSex("M");
ptnt.setPatientParentSpouse("ParentSpuse");
ptnt.setPatientQual("engg");
ptnt.setPatientOccupation("software");
ptnt.setPatientComments("comments here");
System.out.println("Patient = " + ptnt);
//return ptnt.toString();
return ptnt;
}
When I try to access this via browser # localhost:8080/HMS_Web/services/ManagePatient/getPatient
I am getting
javax.servlet.ServletException: org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=text/html, type=class com.hms.app.ui.beans.Patient, genericType=class com.hms.app.ui.beans.Patient.
and I also see the below warning in the logs-
WARNING: A provider com.hms.app.ui.beans.Patient registered in SERVER runtime does not implement any provider interfaces applicable in the SERVER runtime. Due to constraint configuration problems the provider com.hms.app.ui.beans.Patient will be ignored.
If Jersey 2.0 supports JAXB based xml or json support as mentioned # section "8.1.1.2. JAXB based JSON support" in the Jersey guide, I am not sure why I am receiving the Provider errors.
Could any JAX-WS expert help me understand and also provide me direction on how to resolve this situation?
Thank you in advance
you are accessing the service via browser, so your PatientController will try to render response as html, I guess this is the reason for the
javax.servlet.ServletException: org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=text/html, type=class com.hms.app.ui.beans.Patient, genericType=class com.hms.app.ui.beans.Patient.
try to comsume the service via jersey client api as following:
WebTarget webTarget = client.target("http://localhost:8080/HMS_Web/services/ManagePatient/getPatient");
Patient patient = webTarget.request(MediaType.APPLICATION_XML_TYPE).get(Patient.class);
for the warning:
WARNING: A provider com.hms.app.ui.beans.Patient registered in SERVER runtime does not implement any provider interfaces applicable in the SERVER runtime. Due to constraint configuration problems the provider com.hms.app.ui.beans.Patient will be ignored.
I think you should remove:
add(Patient.class);
in your MyApp. Patient is just a POJO, it is neither a resource nor a provider.