且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无法从 VALUE_STRING 中反序列化 java.util.ArrayList 的实例

更新时间:2022-01-10 17:24:24

这是我的老问题的解决方案:

This is the solution for my old question:

我实现了自己的 ContextResolver 以启用 DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY 功能.

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>
{
   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;
   }
}

web.xml 中,我将我的包注册到 servlet 定义中...

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

<servlet>
    <servlet-name>...</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>...;org.lig.hadas.services.mapper</param-value>        
    </init-param>
    ...
</servlet>

...剩下的一切都由球衣/杰克逊透明地完成.

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