How do I use the Jersey JSON POJO support?

前端 未结 11 1829
离开以前
离开以前 2020-11-27 12:24

I have an object that I\'d like to serve in JSON as a RESTful resource. I have Jersey\'s JSON POJO support turned on like so (in web.xml):

  
         


        
11条回答
  •  北海茫月
    2020-11-27 12:55

    Why are you using final fields? I'm using jersey and i have some JAXB objects/pojos and all i had to do was simply annotate my resource method with @Produces("application/json") and it works out of the box. I didn't have to mess with the web.xml. Just make sure your pojos are annotated correctly.

    Here is a simple pojo

    package test;
    
    import javax.xml.bind.annotation.XmlRootElement;
    
    @XmlRootElement
    public class SampleJaxbObject {
    
        private String field1;
    
        private Integer field2;
    
        private String field3;
    
        public String getField1() {
            return field1;
        }
    
        public void setField1(String field1) {
            this.field1 = field1;
        }
    
        public Integer getField2() {
            return field2;
        }
    
        public void setField2(Integer field2) {
            this.field2 = field2;
        }
    
        public String getField3() {
            return field3;
        }
    
        public void setField3(String field3) {
            this.field3 = field3;
        }
    
    
    }
    

提交回复
热议问题