java - spring-mvc 运行报错 No adapter for handler

 王怡君3018 发布于 2022-10-30 11:28

配置了DispatcherServlet的detectAllHandlerMappings属性为true;
spring-mvc.xml也有声明:


    
        
                
        
    


**访问:http://localhost:8080/demo/index 一直报错:**
javax.servlet.ServletException: No adapter for handler [com.wch.controller.UserController@20142abf]: The DispatcherServlet configuration needs to include a HandlerAdapter that supports this handler
org.springframework.web.servlet.DispatcherServlet.getHandlerAdapter(DispatcherServlet.java:1136)

下面是Controller部分:
package com.wch.controller;

import com.wch.entity.User;
import com.wch.service.IUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
public class UserController {

@Resource
private IUserService userService;
@RequestMapping(value = "/index", method = RequestMethod.GET)
public String toIndex(HttpServletRequest request, Model model)
{
    String name = (String)request.getParameter("name");
    User user = this.userService.getUser(name);
    model.addAttribute("user",user);
    return  "index";
}

}

2 个回答
  • 这样配置应该是没问题的

        <!-- 默认的注解映射的支持 -->
        <mvc:annotation-driven />
        <!-- 避免IE在ajax请求时,返回json出现下载 -->
        <bean id="jacksonMessageConverter"
            class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
            <property name="supportedMediaTypes">
                <list>
                    <value>text/html;charset=UTF-8</value>
                    <value>application/json;charset=UTF-8</value>
                </list>
            </property>
        </bean>
    2022-11-12 01:49 回答
  • 知道原因了,原来我的controller里面使用了 @RequestMapping(value = "/index", method = RequestMethod.GET)这个注解,然后我的spring-mvc配置文件也注册了一个handlerAdapter,去掉这段就好了

    <!--<bean-->
            <!--class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">-->
        <!--<property name="messageConverters">-->
            <!--<list>-->
                <!--<ref bean="mappingJacksonHttpMessageConverter" />    &lt;!&ndash; JSON转换器 &ndash;&gt;-->
            <!--</list>-->
        <!--</property>-->
    <!--</bean>
    具体原因说不上,还在研究
    
    
    2022-11-12 01:49 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有