Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>Detailed Reply includes <strong>Server</strong> and <strong>Client</strong> sample implementation with <strong>JSON Marshalling and Unmarshalling</strong></p> </blockquote> <p><strong>Note:</strong> Json POJO Mapping features is done using Jackson</p> <p>I spent a whole day in finidng why message body write was not found. What I was doing wrong is I was using <code>JAXB javax.xml.bind.annotation @XMLRootElement</code> in my Jersey 1.17.1 Web Service and I was trying to unmarshall it with Jackson.</p> <p>Acutally if you Google it you will find that <code>JAXB</code> is only good for XML but for JSON Jackson is excellent. I also forgot to put some configuration paramters in my <code>web.xml</code> that enable POJO Mapping feature.</p> <p>Here is the snap of how you your servlet mapping should be to enable POJO mapping feature of Jackson.</p> <pre><code>&lt;!-- WebService --&gt; &lt;servlet&gt; &lt;servlet-name&gt;REST Service&lt;/servlet-name&gt; &lt;servlet-class&gt;com.sun.jersey.spi.container.servlet.ServletContainer&lt;/servlet-class&gt; &lt;init-param&gt; &lt;param-name&gt;com.sun.jersey.spi.container.ContainerRequestFilters&lt;/param-name&gt; &lt;param-value&gt;com.sun.jersey.api.container.filter.LoggingFilter;com.algo.server.webservice.WebServiceRequestFilter&lt;/param-value&gt; &lt;/init-param&gt; &lt;init-param&gt; &lt;param-name&gt;com.sun.jersey.config.property.packages&lt;/param-name&gt; &lt;param-value&gt;com.algo.server.webservice;org.codehaus.jackson.jaxrs&lt;/param-value&gt; &lt;/init-param&gt; &lt;init-param&gt; &lt;param-name&gt;com.sun.jersey.api.json.POJOMappingFeature&lt;/param-name&gt; &lt;param-value&gt;true&lt;/param-value&gt; &lt;/init-param&gt; &lt;load-on-startup&gt;1&lt;/load-on-startup&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;REST Service&lt;/servlet-name&gt; &lt;url-pattern&gt;/rest/*&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; </code></pre> <p>You also need to add those jar files into your WEB-INF/libs folder</p> <ul> <li>jackson-core-asl-1.9.2.jar</li> <li>jackson-mapper-asl-1.9.2.jar</li> <li>jackson-xc-1.9.2.jar</li> <li>jackson-jaxrs-1.9.2.jar</li> </ul> <p>This is a sample web service method that returns a list of some objects</p> <pre><code> @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&lt;Task&gt; list = new ArrayList&lt;Task&gt;(10); Task task = null; for (int i = 0; i &lt; 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&lt;List&lt;Task&gt;&gt; entity = new GenericEntity&lt;List&lt;Task&gt;&gt;(list) { }; return Response.ok(entity).build(); } </code></pre> <blockquote> <p>Client Side</p> </blockquote> <p>Now How to use convert this JSON object on client side into same <code>List&lt;T&gt;</code> Object. It's a sinch :-)</p> <p><strong>You need to put the same class from the server that you converted into POJO. It shoulb be the same</strong></p> <pre><code>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&lt;Task&gt; lists = clientResponse.getEntity(new GenericType&lt;List&lt;Task&gt;&gt;() {}); System.out.println(lists); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload