spring系列相关注解说明

其一

@RequestMapping用法详解

测试 @RequestMapping 中的 value 和 path 属性(这两个属性作用相同,可以互换,如果仅有这一个属性,则可以省略,下面两个例子均采用省略的方式)

1)将 @RequestMapping 注解在 login 方法上,而UserController上不添加 @RequestMapping 注解,这时的请求 URL 是相对于 Web 根目录

    @Controller
    public class UserController {
     
    	@RequestMapping("/login")
    	public String login() {
    		return "null";
    	}
    }

这时的方法 login() 能处理的 URL 请求路径是基于 Web 应用的,也就是 http://localhost/SpringMVC/login,也就是 index.jsp 页面中的 User Login 链接地址应该是:

<a href="login">User Login</a>

 

2)将 @RequestMapping 注解在 UserController 类上,这时类的注解是相对于 Web 根目录,而方法上的是相对于类上的路径

    @Controller
    @RequestMapping("/user")
    public class UserController {
     
    	@RequestMapping("/login")
    	public String login() {
    		return "null";
    	}
    }

这时的方法login()能处理的 URL 请求路径则是 http://localhost/SpringMVC/user/login,也就是 index.jsp 页面中的 User Login 链接地址应该是:

<a href="user/login">User Login</a>



其二

通过 @RequestMapping 中的 headers 属性,可以限制客户端发来的请求
	@RequestMapping(path = "/login", headers="host=ip:8080")

其三


@Controller
@RequestMapping(path = "/user")(上一级目录)
public class UserController {
        // 该方法将同时接收通过GET和POST方式发来的请求
        :path下一级目录
        :两种请求方式
	@RequestMapping(path = "/login", method={RequestMethod.POST,RequestMethod.GET})
	public String login() {
		return "null";
	}
}


其四

@PathVariable("xxx")
通过 @PathVariable 可以将URL中占位符参数{xxx}绑定到处理器类的方法形参中@PathVariable(“xxx“) 


其五

@RestController
public class CityRestController {

    @Autowired
    private CityService cityService;


    @RequestMapping(value = "/api/city/{id}", method = RequestMethod.GET)
    public City findOneCity(@PathVariable("id") Long id) {
        return cityService.findCityById(id);
    }

    @RequestMapping(value = "/app/mass/{province_id}",method = RequestMethod.GET)
    public City findByIdempotence_id(@PathVariable("province_id") long province_id){
        return cityService.province_id(province_id);
    }


    @RequestMapping(value = "/api/city", method = RequestMethod.POST)
    public void createCity(@RequestBody City city) {
        cityService.saveCity(city);
    }

    @RequestMapping(value = "/api/city", method = RequestMethod.PUT)
    public void modifyCity(@RequestBody City city) {
        cityService.updateCity(city);
    }

    @RequestMapping(value = "/api/city/{id}", method = RequestMethod.DELETE)
    public void modifyCity(@PathVariable("id") Long id) {
        cityService.deleteCity(id);
    }
}




6
@RequestBody的使用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值