• 添加 @ResponseBody出现错误的问题


    maven配置:

    <dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-core-asl</artifactId>
    <version>1.9.12</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-mapper-asl -->
    <dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.12</version>
    </dependency>

    代码:
    @RequestMapping("/user/create")
    @ResponseBody
    public User createUser(){
    User user=new User();
    user.setEmail("@192777585@qq.com");
    user.setFirstname("hello tom");
    user.setLastname("good name");
    user.setPhone(123145);
    return user;
    }

    出现错误:
    org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: No converter found for return value of type: class com.shangsheng.entity.User
    	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)
    	org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
    	org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    	org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
    	org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    

    Root Cause

    java.lang.IllegalArgumentException: No converter found for return value of type: class com.shangsheng.entity.User
    	org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:187)
    	org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:174)
    	org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:81)
    	org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:113)
    	org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
    	org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandler

    解决问题:
    1.修改maven,去掉之前json配置,添加以下配置

    <!--apache shiro end-->
    <!-- 映入JSON -->
    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
    <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
    <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.9.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
    <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.9.2</version>
    </dependency>





  • 相关阅读:
    按照指定的字符串拆分字符串,split()方法。
    charAt()取出指定位置的字符 .length()得到一个字符串的长度 indexOf()查找一个指定的字符是否存在并返回其位置 trim()去掉字符串的左右空格 substring()字符串的截取 str.replace(" ", ""); 去掉所有空格,包括首尾、中间
    字符串与字符数组的多种转换方式。
    匿名对象。
    构造方法。
    递归的练习,1.统计文件夹大小 2.删除文件夹及文件夹下的文件
    jquery零散小饼干
    jQuery review
    git解决冲突
    url、href、src
  • 原文地址:https://www.cnblogs.com/shanshen/p/9188685.html
Copyright © 2020-2023  润新知