0

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.

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

The best way to do so is by mapping your HashMap into a simple bean and return the bean instead of the HashMap.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.