Java.util.Map to JSON object with Jersey / JAXB / Jackson

I am trying to create a REST web service in Jersey. I want to get and select JSON objects from Java classes, for example:

@XmlRootElement public class Book { public String code; public HashMap<String, String> names; } 

This should be converted to JSON as follows:

 { "code": "ABC123", "names": { "de": "Die fabelhafte Welt der Amelie", "fr": "Le fabuleux destin d'Amelie Poulain" } } 

However, I cannot find a standard solution for this. It seems that everyone is implementing their own wrapper solution . This requirement seems extremely basic to me; I can’t believe that this is a generally accepted solution for this, especially since Jersey is really one of the most interesting parts of Java.

I also tried upgrading to Jackson 1.8, which only gives me this, which is extremely inconvenient for JSON:

 { "code": "ABC123", "names": { "entry": [{ "key": "de", "value": "Die fabelhafte Welt der Amelie" }, { "key": "fr", "value": "Le fabuleux destin d'Amelie Poulain" }] } } 

Are there any suggested solutions for this?

+23
java json rest jackson jersey
Apr 26 '11 at 18:22
source share
4 answers

I don’t know why this is not the default, and it took me a while to figure it out, but if you want to work with JSON conversion with Jersey, add

  <init-param> <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name> <param-value>true</param-value> </init-param> 

into your web.xml and all your problems should be resolved.

PS: you also need to get rid of @XmlRootElement annotations to make it work

+22
12 '11 at 7:58
source share

You can use google-gson . Here is a sample code:

  @Test public void testGson(){ Book book = new Book(); book.code = "1234"; book.names = new HashMap<String,String>(); book.names.put("Manish", "Pandit"); book.names.put("Some","Name"); String json = new Gson().toJson(book); System.out.println(json); } 

Output signal {"code":"1234","names":{"Some":"Name","Manish":"Pandit"}}

+7
Apr 26 '11 at 18:52
source share

I know this was asked a long time ago, but everything changed on average, so for the latest Jersey v2.22, which no longer has the com.sun.jersey package, these two dependencies added to the pom.xml project solved the same problem:

 <dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-json-jackson</artifactId> <version>2.22</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.jaxrs</groupId> <artifactId>jackson-jaxrs-json-provider</artifactId> <version>2.5.4</version> <!-- jackson version used by jersey v2.22 --> </dependency> 

No need to add anything to web.xml. The first dependency will instruct jersey to use jackson to convert POJO to JSON. The second dependency will register Jackson as a JSON provider in Jersey.

Also for the null task in POJO, add this annotation to the POJO class:

 @JsonInclude(JsonInclude.Include.NON_NULL) 
+4
May 25 '16 at 14:29
source share
 @POST @Consumes("application/json") public void createBook(Book book) { ..... ..... } 

Of course, you need to have getter / setter for each property in the book.

Also, the reason it is recommended to use a wrapper class (usually a map) is to avoid creating multiple DTOs for all the data types that you want to send. It is easier to simply serialize / deserialize using a map and, as part of the business logic, convert it to the appropriate POJO for internal processing, especially if you use this POJO for relational object mapping.

-2
May 11 '11 at 7:34 a.m.
source share



All Articles