以下是重要信息。使用泽西岛1.6。序列化同一类型的列表到前端没有问题,不同类型的列表不会导致应用程序中其他地方来回出现问题,所以我认为问题在于bean本身。我已经尝试了这个没有额外的路径参数,我遵循所有的bean约定,数据在ajax之前看起来很好......真的不知道我的问题在哪里。除了它是NPE之外,没有其他信息。
豆
public class TheBean implements Serializable{
private static final long serialVersionUID = 1L;
private String beanId;
private String name;
private String beanType;
private Boolean group;
public TheBean(){}
public TheBean(String beanId, String name, String beanType, boolean isGroup) {
super();
this.beanId = beanId;
this.name = name;
this.beanType = beanType;
this.group = isGroup;
}
public String getBeanId() {
return beanId;
}
public void setBeanId(String beanId) {
this.beanId = beanId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBeanType() {
return beanType;
}
public void setBeanType(String beanType) {
this.beanType = beanType;
}
public boolean isGroup() {
return group;
}
public void setGroup(boolean group) {
this.group = group;
}
}
网络服务
@POST
@Path("/update/{parentId}")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response updateTheBeanList(ArrayList<TheBean> routeList, @PathParam("parentId") String parentId){
//code
}
客户端代码
var beanListArray = beanListTable.fnGetData();//looks good here
var json = JSON.stringify(beanListArray);//and here
$.ajax({
dataType: 'text',
type: "POST",
data: json,
url: '${pageContext.request.contextPath}/rest/beanList/update/' + parentId, //looks good here
});
异常
java.lang.NullPointerException
at org.codehaus.jackson.xc.JaxbAnnotationIntrospector.findAdapter(JaxbAnnotationIntrospector.java:1058)
at org.codehaus.jackson.xc.JaxbAnnotationIntrospector.findDeserializer(JaxbAnnotationIntrospector.java:644)
at org.codehaus.jackson.xc.JaxbAnnotationIntrospector.findDeserializer(JaxbAnnotationIntrospector.java:74)
at org.codehaus.jackson.map.AnnotationIntrospector.findDeserializer(AnnotationIntrospector.java:634)
at org.codehaus.jackson.map.AnnotationIntrospector$Pair.findDeserializer(AnnotationIntrospector.java:1286)
有问题的依赖项
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-bundle</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-multipart</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.8.0</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.6</version>
</dependency>
答案 0 :(得分:1)
在你的情况下,Jackson正在使用类org.codehaus.jackson.xc.JaxbAnnotationIntrospector
来反序列化/序列化ArrayList与您的POJO(我假设您在web.xml中配置了com.sun.jersey.api.json.POJOMappingFeature
。)
杰克逊正在寻找一种不存在的注释。见JaxbAnnotationIntrospector.java
中的第643行:
Class potentialAdaptee = ((Member)am.getAnnotated()).getDeclaringClass();
其中am.getAnnotated()
为null
。
因为你没有使用@ XmlRootElement&#34;注释任何bean。一个简单的解决方案是禁用JaxbAnnotationIntrospector
。这可以通过注册提供自己的ContextResolver
实例的自定义ObjectMapper
来完成。
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig.Feature;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;
@Provider
public class PojoObjectMapperProvider implements ContextResolver<ObjectMapper> {
final ObjectMapper defaultObjectMapper;
public PojoObjectMapperProvider() {
defaultObjectMapper = new ObjectMapper();
defaultObjectMapper.configure(Feature.INDENT_OUTPUT, true);
}
@Override
public ObjectMapper getContext(Class<?> type) {
return defaultObjectMapper;
}
}
注册该课程:
import javax.ws.rs.core.Application;
import java.util.HashSet;
import java.util.Set;
public class MyApplication extends Application {
@Override
public Set<Class<?>> getClasses() {
final Set<Class<?>> classes = new HashSet<>();
classes.add(PojoObjectMapperProvider.class);
return classes;
}
}
如果您不使用web.xml:
ApplicationAdapter rc = new ApplicationAdapter(new MyApplication());
rc.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
SelectorThread threadSelector = GrizzlyServerFactory.create(BASE_URI, rc);