users@jersey.java.net

[Jersey] a problem about ContextResolver<JAXBContext>

From: nidongshengnds <nidongshengnds_at_163.com>
Date: Fri, 21 Sep 2012 16:58:10 +0800


I am so sorry to bother you, I encountered a problem that spent me two days on solving it, Unfortunately it is still unsolved. So I had to ask for your help.
 
Problem as follows, when my resource method returns json data ,“JAXBContextResolver implements ContextResolver<JAXBContext>” that I custom doesn’t work. On the contrary, when my resource method returns xml data it works.
The following is my resource:
         @GET
         @Path("book")
         @Produces(MediaType.APPLICATION_JSON)
         public BookDto getBookJSON(){
                   BookDto dto = new BookDto();
                   dto.setId(1);
                   dto.setIssueDate(new Date());
                   dto.setLocation("123456");
                   dto.setName("4321412");
                   dto.setNote("note");
                   dto.setPageNumber(12);
                   dto.setStatus("1");
                   return dto;
         }
The following is my entity class
@XmlRootElement
public class BookDto {
         private Integer id;
         
         private String name;
         
         private Integer pageNumber;
         
         private String location;
         
         private String note;
         
         private String status;
         
         private Date issueDate;//issueDate
         ...getter & setters
}
The following is my custom context class:
@Provider
public class JAXBContextResolver implements ContextResolver<JAXBContext> {
 
         private JAXBContext context;
 
         private Class<?>[] types = {BookDto.class};
         
    public JAXBContextResolver() throws Exception {
        this.context =new JSONJAXBContext(JSONConfiguration.mapped().attributeAsElement("location").build(),types);
    }
 
         @Override
         public JAXBContext getContext(Class<?> objectType) {
                   for (Class<?> type : types) {
            if (type == objectType) {
                return context;
            }
        }
        return null;
         }
}
 When my resource method returns BookDto instance, it dosen’t trigger the getContext method of JAXBContextResolver.
 Instructions: when the system starts, resource class and @ Provider are both successfully read.
 Environment:
   win7 32-bit operating system
   eclipse Helios Release
    tomcat 6.0
    jersey integrating with spring
2012-09-21



nidongshengnds