本文介绍了我正在使用spring rest api开发一个示例应用程序。但我得到一个错误,我认为它与杰克逊图书馆有关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到的错误:

Feb 04, 2016 9:20:52 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [dispatcher] in context with path
[/Example] threw exception [Request processing failed; nested exception is java.lang.IllegalArgumentException: Unrecognized Type: [null]] with rootcause
java.lang.IllegalArgumentException: Unrecognized Type: [null]
atcom.fasterxml.jackson.databind.type.TypeFactory._fromAny(TypeFactory.java:1109)
at com.fasterxml.jackson.databind.type.TypeFactory.constructType(TypeFactory.java:566)
at com.fasterxml.jackson.databind.type.TypeFactory.constructType(TypeFactory.java:602)
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.getJavaType(AbstractJackson2HttpMessageConverter.java:311)
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:249)
at org.springframework.http.converter.AbstractGenericHttpMessageConverter.write(AbstractGenericHttpMessageConverter.java:100)
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:222)
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:153)
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:165)
at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:80)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:126)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:814)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:737)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:969)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:860)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:845)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)

用户实体类:

@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;

private String firstName;
private String lastName;
private String userName;

public long getId() {
    return id;
}

public void setId(long id) {
    this.id = id;
}

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public String getUserName() {
    return userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}
}

存储库类:

@Repository
public class UserRepositoryImpl implements UserRepository {

@PersistenceContext
EntityManager entityManager;

public User findOne(long id) {
    User user = entityManager.find(User.class, id);
    return user;
}

public User createOne(User user) {
    entityManager.persist(user);
    return user;
}

public void deleteOne(long id) {
    User user = findOne(id);
    entityManager.remove(user);
}

public User updateOne(User user) {
    return entityManager.merge(user);
}

@SuppressWarnings("unchecked")
public List<User> getAll() {
    Query getAllQuery = entityManager.createNamedQuery("select user from User as user");
    return (List<User>) getAllQuery.getResultList();
}

}

服务类:

@Service
@Transactional
public class UserServiceImpl implements UserService {

@Autowired
private UserRepository userRepository;

public User createOne(User user) {
    return userRepository.createOne(user);
}

public User updateOne(User user) {
    return userRepository.updateOne(user);
}

public void deleteOne(long id) {
    userRepository.deleteOne(id);
}

public User findOne(long id) {
    System.out.println(id);
    return userRepository.findOne(id);
}

public List<User> getAll() {
    return userRepository.getAll();
}

}

控制器类:

@RestController
@RequestMapping(value = "/user")
public class UserController {

@Autowired
private UserService userService;

@RequestMapping(method = RequestMethod.POST)
public User createOne(@RequestBody User user) {
    return userService.createOne(user);
}

@RequestMapping(method = RequestMethod.PUT)
public User updateOne(@RequestBody User user) {
    return userService.updateOne(user);
}

@RequestMapping(path = "/{id}", method = RequestMethod.GET)
public User findOne(@PathVariable("id") long id) {
    User user = userService.findOne(id);
    return user;
}

@RequestMapping(value = "/{id}")
public void deleteOne(@PathVariable("id") Long id) {
    userService.deleteOne(id);
}
}

pom.xml文件:

The pom.xml file:

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.adaptiv.exampleapp</groupId>
<artifactId>Example</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Example Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>

    <!-- Spring -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.2.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.2.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.2.4.RELEASE</version>
    </dependency>

    <!-- Servlet -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
    </dependency>

    <!-- Spring ORM -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>4.2.4.RELEASE</version>
    </dependency>

    <!-- Hibernate JPA -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>5.0.7.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.1-api</artifactId>
        <version>1.0.0.Final</version>
    </dependency>

    <!-- MySql Driver -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.38</version>
    </dependency>

    <!-- Jackson -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.7.1</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.7.1</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.7.1</version>
    </dependency>
    <!-- JSON -->
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20151123</version>
    </dependency>

</dependencies>
<build>
    <finalName>Example</finalName>
</build>
</project>

知道为什么会出现这个错误吗?

如果我做了帖子请求到UserController,User对象保存在db中,然后发生该错误。
配置正确完成我认为,调度程序serlvet配置为扩展 AbstractAnnotationConfigDispatcherServletInitializer

Any idea why this error is occurring?
If i do the post request to UserController the User object is saved in db and then that error occur.The configurations are done correctly i think, dispatcher serlvet is configured extending AbstractAnnotationConfigDispatcherServletInitializer

EDIT:
When i stop the server the following warning appears:

WARNING: The web application [Example] appears to have started a thread named
[Abandoned connection cleanup thread] but has failed to stop it.
This is very likely to create a memory leak. Stack trace of thread:
java.lang.Object.wait(Native Method)
java.lang.ref.ReferenceQueue.remove(Unknown Source)
com.mysql.jdbc.AbandonedConnectionCleanupThread.run(AbandonedConnectionCleanupThread.java:43)


推荐答案

请检查你的版本你的pom.xml上的jackson dependecies,试着改变它:)

Please check your version of jackson dependecies on your pom.xml , try to change it :)

这篇关于我正在使用spring rest api开发一个示例应用程序。但我得到一个错误,我认为它与杰克逊图书馆有关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 17:41