浅谈@RequestMapping注解的注意点
作者:zhangzengxiu 发布时间:2023-07-11 02:12:39
@RequestMapping注解注意点
类上加没加@RequestMappin注解区别
1.如果类上加了 @RequestMappin注解,那么就会去该注解对应的路径下去找页面,如果没有对应的页面就会报错。
举例说明:
@RequestMapping("/user")
public class UserController {
@RequestMapping("/requestParam51")
public String requestParam51(String[] name) {
return "index.jsp";
}
}
对应的跳转页面会去user目录下去找,找不到就会报错。
2.如果类上没有加@RequestMapping注解,就会直接去根路径下去找页面
3.如果为跳转的页面加了"/",还是会去根路径下去找对应的页面。
举例:
@RequestMapping("/user")
public class UserController {
@RequestMapping("/requestParam51")
public String requestParam51(String[] name) {
return "/index.jsp";
}
}
@RequestMapping一个坑
今天发现了RequestMapping注解的一个坑:
当RequestMapping用于Class上时,不能用1.0,v1.0这样带小数点的value值做开头
@Controller
@RequestMapping(value = "/v1.0")
public class TestController {
@RequestMapping(value = "/a", method = RequestMethod.GET, produces = "application/json")
public @ResponseBody
Object getA() {
return "{\"test\" : \"a\"}";
}
@RequestMapping(value = "/b", method = RequestMethod.GET, produces = "application/json")
public @ResponseBody
Object getB() {
return "{\"test\" : \"b\"}";
}
}
如上代码运行后,访问http://localhost:port/v1.0/a 或者http://localhost:port/v1.0/b 时都会报错:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0': Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping found. Cannot map 'testController' bean method
public java.lang.Object com.my.test.controller.TestController.getB()
to {[/v1.0],methods=[GET],params=[],headers=[],consumes=[],produces=[application/json],custom=[]}: There is already 'testController' bean method
单看异常信息,还以为是有重名的路径,结果搜遍了工程也没找到重名的类,后来"v1.0"改成"v1",就正常运行了。
顺带测试了下,发现改成1.0也是同样的错误。
之后再把一个方法上RequestMapping的value去掉,采用默认写法:
@RequestMapping("/b")
public @ResponseBody
Object getB() {
return "{\"test\" : \"b\"}";
}
再运行起来,访问http://localhost:port/v1.0/a或者http://localhost:port/v1.0/b 就会变成404错误。
HTTP Status 404 - /v1.0/a
type Status report
message /v1.0/a
description The requested resource is not available.
没深究根本原因,估计是Spring的小bug,以后避免带小数点的路径头。
来源:https://blog.csdn.net/zhangzengxiu/article/details/121585653


猜你喜欢
- 一、界面部分:首先,打开visual studio新建项目;然后使用“工具箱”添加控件:分别添加button,datagridview,te
- 首先看两段代码,一段是Integer的,一段是AtomicInteger的,为以下:public class Sample1 {  
- 很多核心 Java 面试题来源于多线程(Multi-Threading)和集合框架(Collections Framework),理解核心线
- 1.Android 连接MySQL数据库public class DBOpenHelper {private static String d
- 前言碎语Disruptor是英国LMAX公司开源的高性能的线程间传递消息的并发框架,和jdk中的BlockingQueue非常类似,但是性能
- RFC6749OAuth2的官方文档在RFC6749:https://datatracker.ietf.org/doc/html/rfc67
- 背景为了了解Seata AT模式的原理,我通过源码解读的方式画出了Seata AT模式启动的图示:如果是基于Springboot项目的话,项
- 本文实例讲述了Android中Bitmap用法。分享给大家供大家参考,具体如下:一般在android程序中把图片文件放在res/drawab
- 推荐阅读idea官网下载链接(对应版本号下载):https://www.jetbrains.com/idea/download/other.
- 一、判断字母string str = Console.ReadLine();if (char.isLetter(str)){}else if
- 最近该忙的都忙完了,自己自定义一直是个弱项,也一直想整个钟表玩玩,网上看了一圈,学习了不少,下面自己做做自定义首先,制作钟表第一步,肯定是画
- 本文实例为大家分享了Android Studio实现登录界面的具体代码,供大家参考,具体内容如下题目设计一个登录界面。要求:a) 包含用户名
- 前言 同源策略:判断是否是同源的,主要看这三点,协议,ip,端口。同源策略就是浏览器出于网站安全性的考虑,限制不同源之间的资源相互访问的一种
- 一、是什么当下很多公司都采取前后端分离的开发模式,前端和后端的工作由不同的工程师完成。在这种开发模式下,维持一份及时更新且完整的 Rest
- 1.添加引用工具箱---右键---选择项--COM组件--Adobe PDF Reader2.使用方法OpenFileDialog open
- CXF简介CXF是一个开源的WebService框架。Apache CXF = Celtix + XFire,开始叫 Apache Celt
- 一、介绍Properties文件在Java中主要为配置文件,文件类型为:.properties,格式为文本文件,内容格式为"键=值
- 具体代码如下所示:***web.xml***<?xml version="1.0" encoding="
- 导入后gradle building 一直到跑,卡住了,一般是gradle没有下载,又下不下来的原因。去 http://serv
- 本文实例为大家分享了Java执行SQL脚本文件到数据库的具体方式,供大家参考,具体内容如下方式一:直接读取SQL脚本文件的内容,然后传递到S