Can not deserialize instance of java.util.ArrayList out of VALUE_STRING

前端 未结 5 1662
谎友^
谎友^ 2020-11-30 04:41

I have a REST service built with Jersey and deployed in the AppEngine. The REST service implements the verb PUT that consumes an application/json media type. Th

5条回答
  •  醉梦人生
    2020-11-30 04:52

    This is the solution for my old question:

    I implemented my own ContextResolver in order to enable the DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY feature.

    package org.lig.hadas.services.mapper;
    
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.MediaType;
    import javax.ws.rs.ext.ContextResolver;
    import javax.ws.rs.ext.Provider;
    
    import org.codehaus.jackson.map.DeserializationConfig;
    import org.codehaus.jackson.map.ObjectMapper;
    
    @Produces(MediaType.APPLICATION_JSON)
    @Provider
    public class ObjectMapperProvider implements ContextResolver
    {
       ObjectMapper mapper;
    
       public ObjectMapperProvider(){
           mapper = new ObjectMapper();
           mapper.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
       }
       @Override
       public ObjectMapper getContext(Class type) {
           return mapper;
       }
    }
    

    And in the web.xml I registered my package into the servlet definition...

    
        ...
        com.sun.jersey.spi.container.servlet.ServletContainer
        
            com.sun.jersey.config.property.packages
            ...;org.lig.hadas.services.mapper        
        
        ...
    
    

    ... all the rest is transparently done by jersey/jackson.

提交回复
热议问题