@ControllerAdvice

helloworlda 发布于 2017/08/05 10:44
阅读 171
收藏 0

看《看透springMvc源代码分析与实践》@ControllerAdvice中例子 不能正常运行,访问时报404,不知道什么原因

 

代码:

@ControllerAdvice
public class HahaResponseBodyAdvice implements ResponseBodyAdvice<String> {
    @Override
    public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> con-verterType) {
        return true;
    }

    @Override
    public String beforeBodyWrite(String body, MethodParameter returnType, MediaType selectedContentType,
                                  Class<? extends HttpMessageConverter<?>> selectedConverterType,
                                  ServerHttpRequest request, ServerHttpResponse response) {
        return body+"<br/> haha,this is been modified";
    }
}

//com.excelib.controller.GoController
//在处理器返回值前暂时添加@ResponseBody注释
@RequestMapping(value={"/index","/"},method= {RequestMethod.GET})
@ResponseBody public String index(HttpServletRequest request, Model model) throws Exception {
   model.addAttribute("msg", "Go Go Go!");
   return "go.jsp";
}

springmvc 配置文件


    <mvc:annotation-driven />
        <context:component-scan base-package="com.happy1"   >
    </context:component-scan>

加载中
0
伊人梦醉
伊人梦醉

需要maven-demo-web?你用的tomcat部署的?

不是的话,去掉试试。

0
helloworlda
helloworlda

是的,这是我写demo项目

OSCHINA
登录后可查看更多优质内容
返回顶部
顶部