gpt4 book ai didi

jackson - JsonMappingException : (was java. lang.ArrayIndexOutOfBoundsException)

转载 作者:行者123 更新时间:2023-12-05 05:27:40 31 4
gpt4 key购买 nike

我有一个 Web 应用程序,其中服务器以 JSON 格式返回任何结果。为了创建 JSON,我使用 codehaus Jackson ObjectWriter 版本 1.9.8。

我遇到的问题是映射中有时会出现错误,从那时起所有服务器调用都会导致错误。我无法确定导致错误的原因,但我确实发现了根源。

当异常发生时,服务端返回“(was java.lang.ArrayIndexOutOfBoundsException) (through reference chain: com.onior.modm.reSTLet.helpers.ServiceResult["success"])”,表示异常抛出在“toJSON”中被“toRepresentation”捕获并映射到 JSON,否则它将返回空。

我只是不知道为什么它有时会失败。我整个上午都可以毫无问题地使用该应用程序,然后突然出现此错误。它发生在不同的调用上,但这些调用会在其他时间成功。从我的角度来看,这似乎很随意,但也许有人可以帮我看看? :)

被映射的服务器结果:

public class ServiceResult<T> {
private boolean success;
private T results = null;
private ModmServiceStatus message = null;

public ServiceResult() {
}

public ServiceResult(T results) {
this.success = true;
this.results = results;
}

public ServiceResult(boolean success, ModmServiceStatus message) {
this.success = success;
this.message = message;
}

public ServiceResult(ServiceError error) {
this(false, new ModmServiceStatus(error));
}

public static ServiceResult<ModmServiceStatus> serviceException(
ServiceError error) {
return new ServiceResult<ModmServiceStatus>(false,
new ModmServiceStatus(error.getCode(), error.getDescription()));
}

public static ServiceResult<ModmServiceStatus> dbError() {
return ServiceResult
.serviceException(ServiceError.GENERIC_DATABASE_ERROR);
}

public static ServiceResult<ModmServiceStatus> invalidJson() {
return ServiceResult
.serviceException(ServiceError.GENERIC_INVALID_JSON);
}

public static ServiceResult<ModmServiceStatus> missingEntity() {
return ServiceResult .serviceException(ServiceError.GENERIC_MISSING_OR_INCOMPLETE_ENTITY);
}

public static ServiceResult<ModmServiceStatus> entityNotFound() {
return ServiceResult
.serviceException(ServiceError.GENERIC_ENTITY_NOT_FOUND);
}

public static ServiceResult<ModmServiceStatus> entityDeleted(String entity) {
return new ServiceResult<ModmServiceStatus>(true,
new ModmServiceStatus(0, entity + " deleted."));
}
}

映射:

public class RestUtils {
private static final boolean PRETTY_PRINT = true;

public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
public static final ObjectWriter OBJECT_WRITER = (PRETTY_PRINT ? OBJECT_MAPPER
.writerWithDefaultPrettyPrinter() : OBJECT_MAPPER.writer());

@SuppressWarnings("unchecked")
public static <T> JacksonRepresentation<T> toJSON(T t) throws IOException {
JsonRepresentation jsonRepresentation = null;
JacksonRepresentation<T> jacksonRepresentation = null;
jsonRepresentation = new JsonRepresentation(
OBJECT_WRITER.writeValueAsString(t)); // Origin of incidental
// server error
jacksonRepresentation = new JacksonRepresentation<T>(
jsonRepresentation, (Class<T>) t.getClass());
return jacksonRepresentation;
}

public static <T> Representation toRepresentation(ServiceResult<T> ss) {
Representation representation = null;
try {
representation = RestUtils.toJSON(ss);
} catch (IOException jsonException) {
jsonException.printStackTrace();
try {
jsonException.printStackTrace();
representation = RestUtils.toJSON(jsonException.getMessage());
} catch (Exception e) {
e.printStackTrace();
}
}
return representation;
}
}

电话:

RestUtils.toRepresentation(new ServiceResult<List<Group>>(groups));

堆栈跟踪异常:

org.codehaus.jackson.map.JsonMappingException: (was java.lang.ArrayIndexOutOfBoundsException) (through reference chain: com.onior.modm.restlet.helpers.ServiceResult["success"])
at org.codehaus.jackson.map.JsonMappingException.wrapWithPath(JsonMappingException.java:218)
at org.codehaus.jackson.map.JsonMappingException.wrapWithPath(JsonMappingException.java:183)
at org.codehaus.jackson.map.ser.std.SerializerBase.wrapAndThrow(SerializerBase.java:140)
at org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:158)
at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112)
at org.codehaus.jackson.map.ser.StdSerializerProvider._serializeValue(StdSerializerProvider.java:610)
at org.codehaus.jackson.map.ser.StdSerializerProvider.serializeValue(StdSerializerProvider.java:256)
at org.codehaus.jackson.map.ObjectWriter._configAndWriteValue(ObjectWriter.java:456)
at org.codehaus.jackson.map.ObjectWriter.writeValueAsString(ObjectWriter.java:393)
at com.onior.modm.restlet.helpers.RestUtils.toJSON(RestUtils.java:52)
at com.onior.modm.restlet.helpers.RestUtils.toRepresentation(RestUtils.java:71)
at com.onior.modm.restlet.resources.GroupCollectionResource.toJsonRead(GroupCollectionResource.java:191)
at sun.reflect.GeneratedMethodAccessor143.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.restlet.resource.ServerResource.doHandle(ServerResource.java:506)
at org.restlet.resource.ServerResource.get(ServerResource.java:707)
at org.restlet.resource.ServerResource.doHandle(ServerResource.java:589)
at org.restlet.resource.ServerResource.doNegotiatedHandle(ServerResource.java:649)
at org.restlet.resource.ServerResource.doConditionalHandle(ServerResource.java:348)
at org.restlet.resource.ServerResource.handle(ServerResource.java:952)
at org.restlet.resource.Finder.handle(Finder.java:246)
at org.restlet.routing.Filter.doHandle(Filter.java:159)
at org.restlet.routing.Filter.handle(Filter.java:206)
at org.restlet.routing.Router.doHandle(Router.java:431)
at org.restlet.routing.Router.handle(Router.java:648)
at org.restlet.routing.Filter.doHandle(Filter.java:159)
at org.restlet.routing.Filter.handle(Filter.java:206)
at org.restlet.routing.Filter.doHandle(Filter.java:159)
at org.restlet.routing.Filter.handle(Filter.java:206)
at org.restlet.routing.Router.doHandle(Router.java:431)
at org.restlet.routing.Router.handle(Router.java:648)
at org.restlet.routing.Filter.doHandle(Filter.java:159)
at org.restlet.routing.Filter.handle(Filter.java:206)
at org.restlet.routing.Filter.doHandle(Filter.java:159)
at org.restlet.routing.Filter.handle(Filter.java:206)
at org.restlet.routing.Filter.doHandle(Filter.java:159)
at org.restlet.engine.application.StatusFilter.doHandle(StatusFilter.java:155)
at org.restlet.routing.Filter.handle(Filter.java:206)
at org.restlet.routing.Filter.doHandle(Filter.java:159)
at org.restlet.routing.Filter.handle(Filter.java:206)
at org.restlet.engine.CompositeHelper.handle(CompositeHelper.java:211)
at org.restlet.engine.application.ApplicationHelper.handle(ApplicationHelper.java:84)
at org.restlet.Application.handle(Application.java:381)
at org.restlet.ext.servlet.ServletAdapter.service(ServletAdapter.java:206)
at org.restlet.ext.spring.RestletFrameworkServlet.doService(RestletFrameworkServlet.java:124)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ArrayIndexOutOfBoundsException
at org.codehaus.jackson.impl.WriterBasedGenerator.writeRaw(WriterBasedGenerator.java:577)
at org.codehaus.jackson.util.DefaultPrettyPrinter$Lf2SpacesIndenter.writeIndentation(DefaultPrettyPrinter.java:279)
at org.codehaus.jackson.util.DefaultPrettyPrinter.beforeObjectEntries(DefaultPrettyPrinter.java:98)
at org.codehaus.jackson.impl.WriterBasedGenerator._writePPFieldName(WriterBasedGenerator.java:410)
at org.codehaus.jackson.impl.WriterBasedGenerator._writeFieldName(WriterBasedGenerator.java:340)
at org.codehaus.jackson.impl.WriterBasedGenerator.writeFieldName(WriterBasedGenerator.java:217)
at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:444)
at org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150)
... 58 more

最佳答案

得到与 Jackson 1.9.9 完全相同的 ArrayIndexOutOfBoundsException

经过调查,我们的根本原因是多个线程使用了一个共享的 PrettyPrinter。如果您查看 DefaultPrettyPrinter 的 Jackson 代码,您将看到:

/**
* Number of open levels of nesting. Used to determine amount of
* indentation to use.
*/
protected int _nesting = 0;

_nesting 最终被用于访问数组。该变量在处理对象时递增和递减。如果多个线程递减它,它可能会以负数结束,从而导致 ArrayIndexOutOfBoundsException。一旦为负数,它就不会再增加,因为在到达将增加它的代码段之前会生成异常。

在您的代码中,我看到您有一个静态对象编写器。您可能最终得到 DefaultPrettyPrinter 的单个实例。如果您的应用程序可以并发生成 json 对象,只要有足够的时间,您就会得到异常。

斯蒂芬

关于jackson - JsonMappingException : (was java. lang.ArrayIndexOutOfBoundsException),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17924865/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com