Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to send a HashMap object via Jersey webservice framework?
    primarykey
    data
    text
    <p>I have a simple hashmap calss that I want to send as JSON via Jersey. Does anyone have an example on how to perform such an operation , I have tried the following </p> <pre><code>package de.vogella.jersey.first; import java.util.HashMap; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import com.sun.jersey.api.view.Viewable; // This method is called if XML is request @GET @Produces({ MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON }) public HashMap sayXMLHello() { HashMap map = new HashMap(); map.put( new Integer( 2 ), "two" ); map.put( new Integer( 4 ), "four" ); return map; } // This method is called if HTML is request @GET @Produces(MediaType.TEXT_HTML) public String sayHtmlHello() { return "&lt;html&gt; " + "&lt;title&gt;" + "Hello Jersey" + "&lt;/title&gt;" + "&lt;body&gt;&lt;h1&gt;" + "Hello Jersey" + "&lt;/body&gt;&lt;/h1&gt;" + "&lt;/html&gt; "; } } </code></pre> <p>and then the client </p> <pre><code>package de.vogella.jersey.first; import java.net.URI; import java.util.HashMap; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.UriBuilder; import com.sun.jersey.api.client.Client; import com.sun.jersey.api.client.ClientResponse; import com.sun.jersey.api.client.WebResource; import com.sun.jersey.api.client.config.ClientConfig; import com.sun.jersey.api.client.config.DefaultClientConfig; public class Test { public static void main(String[] args) { ClientConfig config = new DefaultClientConfig(); Client client = Client.create(config); WebResource service = client.resource(getBaseURI()); // Calls the HashMap function System.out.println( service.path("rest") .path("hello") .accept(MediaType.APPLICATION_JSON) .get(HashMap.class)); // The HTML System.out.println(service.path("rest") .path("hello") .accept(MediaType.TEXT_HTML) .get(String.class)); } private static URI getBaseURI() { return UriBuilder.fromUri("http://localhost:8080/de.vogella.jersey.first").build(); } } </code></pre> <p>once I deploy and call the client class , I get the following error :</p> <pre><code>Exception in thread "main" com.sun.jersey.api.client.UniformInterfaceException: GET http://localhost:8080/de.vogella.jersey.first/rest/hello returned a response status of 500 Internal Server Error at com.sun.jersey.api.client.WebResource.handle(WebResource.java:686) at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74) at com.sun.jersey.api.client.WebResource$Builder.get(WebResource.java:507) at de.vogella.jersey.first.Test.main(Test.java:25) </code></pre> <p>so any ideas on how can I send a HashMap object from a server to the client using Jersey? also I can't map the HashMap object to a POJO for not having a uniform structure (changes all the time) <p> thanks.</p>
    singulars
    1. This table or related slice is empty.
    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. 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