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

前端 未结 4 966
遥遥无期
遥遥无期 2020-12-08 01:06

I\'ve been trying to create a Jersey REST Webservice. I want to receive and emit JSON objects from Java classes like the following:

@XmlRootElement
public cl         


        
4条回答
  •  不思量自难忘°
    2020-12-08 01:52

    I know it's been asked long time ago, but things changed mean time, so for the latest Jersey v2.22 that do not have anymore the package com.sun.jersey, these two dependencies added in the project pom.xml solved the same problem:

    
      org.glassfish.jersey.media
      jersey-media-json-jackson
      2.22
    
    
      com.fasterxml.jackson.jaxrs
      jackson-jaxrs-json-provider
      2.5.4 
    
    

    No need to add anything in web.xml. First dependency will instruct jersey to use jackson for POJO to JSON transformations. Second dependency will register jackson as jersey JSON provider.

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

    @JsonInclude(JsonInclude.Include.NON_NULL)
    

提交回复
热议问题