我正在使用 Rest Template 在 Maven 项目中使用 Web 服务,并将项目打包到 jar 中以作为对我的其他 Web 应用程序的依赖项添加,但在实例化 RestTemplate 时向控制台抛出异常,即使我放置代码来捕获异常,它也是直接的将以下异常抛出到控制台。谁能知道原因?
try{
RestTemplate restTemplate = new RestTemplate(); //getting the Exception here
}
catch(Exception e){
e.printStackTrace(); // but its not coming here
}
我在 pom.xml 文件中添加的依赖项:
<!-- Spring dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.9.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<!-- JSON-Binding -->
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.8.3</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>1.1.0.1</version>
<scope>provided</scope>
</dependency>
服务器端的异常是:
Root cause of ServletException.
java.lang.NoSuchMethodError: com.fasterxml.jackson.databind.SerializationConfig.withDefaultPrettyPrinter(Lcom/fasterxml/jackson/core/PrettyPrinter;)Lcom/fasterxml/jackson/databind/SerializationConfig;
at com.fasterxml.jackson.dataformat.xml.XmlMapper.<init>(XmlMapper.java:86)
at com.fasterxml.jackson.dataformat.xml.XmlMapper.<init>(XmlMapper.java:67)
at com.fasterxml.jackson.dataformat.xml.XmlMapper.<init>(XmlMapper.java:63)
at org.springframework.http.converter.json.Jackson2ObjectMapperBuilder$XmlObjectMapperInitializer.create(Jackson2ObjectMapperBuilder.java:807)
at org.springframework.http.converter.json.Jackson2ObjectMapperBuilder.build(Jackson2ObjectMapperBuilder.java:585)
Truncated. see log file for complete stacktrace
>