执行此行时出现Null指针异常:
mapper.readTree(..)
在Spring Boot应用程序中。
发生这种情况的应用程序代码是:
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.node.TextNode;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
public class JsonCompare {
private static final ObjectMapper mapper = new ObjectMapper();
private ArrayList<String> newfileerror = new ArrayList<>();
static {
mapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
}
public ArrayList<String> compare(String json1, String json2) throws IOException {
//convert string to node, convert to sorted string, and back to node!!:
JsonNode node1 = mapper.readTree(mapper.writeValueAsString(mapper.treeToValue(mapper.readTree(json1), Object.class)));
JsonNode node2 = mapper.readTree(mapper.writeValueAsString(mapper.treeToValue(mapper.readTree(json2), Object.class)));
compareNodes(node1, node2, "/");
return newfileerror;
}
}
NPException的日志如下:
2018-07-24 13:00:09,027 [http-nio-8080-exec-7] DEBUG org.springframework.web.servlet.DispatcherServlet - Successfully completed request
2018-07-24 13:00:16,129 [http-nio-8080-exec-3] DEBUG org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'dispatcherServlet' processing POST request for [/compare]
2018-07-24 13:00:16,129 [http-nio-8080-exec-3] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Looking up handler method for path /compare
2018-07-24 13:00:16,130 [http-nio-8080-exec-3] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Returning handler method [public org.springframework.web.servlet.ModelAndView co.nat.tool.controllers.JSONCompareController.compare(javax.servlet.http.HttpServletRequest,co.nat.tool.beans.TransformationDetails,org.springframework.validation.BindingResult,org.springframework.ui.Model) throws java.lang.Exception]
2018-07-24 13:00:16,130 [http-nio-8080-exec-3] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'JSONCompareController'
2018-07-24 13:00:16,132 [http-nio-8080-exec-3] DEBUG org.springframework.web.cors.DefaultCorsProcessor - Skip CORS processing: request is from same origin
2018-07-24 13:00:16,153 [http-nio-8080-exec-3] DEBUG org.springframework.beans.CachedIntrospectionResults - Not strongly caching class [co.nat.tool.beans.TransformationDetails] because it is not cache-safe
2018-07-24 13:00:17,136 [http-nio-8080-exec-3] DEBUG org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver - Resolving exception from handler [public org.springframework.web.servlet.ModelAndView co.nat.tool.controllers.JSONCompareController.compare(javax.servlet.http.HttpServletRequest,co.nat.tool.beans.TransformationDetails,org.springframework.validation.BindingResult,org.springframework.ui.Model) throws java.lang.Exception]: java.lang.NullPointerException
2018-07-24 13:00:17,139 [http-nio-8080-exec-3] DEBUG org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver - Resolving exception from handler [public org.springframework.web.servlet.ModelAndView co.nat.tool.controllers.JSONCompareController.compare(javax.servlet.http.HttpServletRequest,co.nat.tool.beans.TransformationDetails,org.springframework.validation.BindingResult,org.springframework.ui.Model) throws java.lang.Exception]: java.lang.NullPointerException
2018-07-24 13:00:17,146 [http-nio-8080-exec-3] DEBUG org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Resolving exception from handler [public org.springframework.web.servlet.ModelAndView co.nat.tool.controllers.JSONCompareController.compare(javax.servlet.http.HttpServletRequest,co.nat.tool.beans.TransformationDetails,org.springframework.validation.BindingResult,org.springframework.ui.Model) throws java.lang.Exception]: java.lang.NullPointerException
2018-07-24 13:00:17,150 [http-nio-8080-exec-3] DEBUG org.springframework.web.servlet.DispatcherServlet - Could not complete request
java.lang.NullPointerException
at com.fasterxml.jackson.core.JsonFactory.createParser(JsonFactory.java:889)
at com.fasterxml.jackson.databind.ObjectMapper.readTree(ObjectMapper.java:2551)
at co.nat.tool.cma1.JsonCompare.compare(JsonCompare.java:31)
当应用程序试图执行方法“ mapper.readTree”时,它给了我这个例外。我不明白为什么这个异常会发生,因为我是这个春季靴子的新手。 我尝试了很多事情(为杰克逊添加配置以用于序列化功能的春季启动,因为它已映射到代码中),但它们无法正常工作。
Spring启动配置如下:
import javax.ws.rs.ext.ContextResolver;
import org.springframework.context.annotation.Configuration;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
@Configuration
public class JacksonConfiguration implements ContextResolver<ObjectMapper>{
private final ObjectMapper objectMapper;
public JacksonConfiguration() {
objectMapper = new ObjectMapper();
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
objectMapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
}
@Override
public ObjectMapper getContext(Class<?> type) {
return objectMapper;
}
}
任何人都可以告诉我我在做什么错,以及我应该采取什么措施来解决此问题。 我的任务是将Web应用程序(已经工作)迁移到Spring Boot应用程序,因此我无法更改Java代码,因为它之前一直在工作。我的想法是如上所述将配置添加到spring boot配置文件中。