There are lots of questions on returning serialized HashMap, but I didn't find a very good answer.
Now I just want to simply create a rest service like this:
@XmlElement(name = "Calibration")
private HashMap<String, Double> entry = new HashMap<String, Double>();
Then in my REST service:
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/entry")
public HashMap<String, Double> current() {
System.out.println("calibration request");
entry.put("test", 3.00);
return entry;
}
It throws javax.ws.rs.WebApplicationException when the service is called:
com.sun.jersey.api.MessageException: A message body writer for Java class java.util.HashMap, and Java type java.util.HashMap, and MIME media type application/json was not found.
My pom.xml, in case it is useful:
<dependency>
<groupId>com.sun.grizzly</groupId>
<artifactId>grizzly-servlet-webserver</artifactId>
<version>1.9.18-i</version>
</dependency>
<dependency>
<groupId>com.sun.grizzly</groupId>
<artifactId>grizzly-comet-webserver</artifactId>
<version>1.9.46</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-grizzly</artifactId>
<version>1.9.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.9.1</version>
</dependency>
I'm using Grizzly as webContainer.
Jackson would be probably the easiest choice here (and Jersey JSON module has Jackson support built-in). You can configure the POJO JSON support via web.xml
or via configuration object (see POJO support ).
Note: The latest Jersey 1.x version is 1.17.1, consider using this version if it's possible for you.
最好的方法是将HashMap映射到一个简单的bean中,然后返回该bean,而不是HashMap。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.