赞
踩
在Web项目开发中,通常会使用GET类型请求访问查询接口,使用POST类型请求访问保存方法,@RequestMapping注解可以为接口设置访问类型
@Controller
public class HelloController {
@RequestMapping(value = "/hello",method = {RequestMethod.GET,RequestMethod.POST})
public String hello(){
return "index";
}
}
除了使用method属性设置接口访问类型外,SpringMVC框架还提供了@GetMapping、@PostMapping等注解实现类似功能。如下接口如果使用POST以外的请求类型进行访问,就会报错
@Controller
public class HelloController {
@PostMapping("/hello")
public String hello(){
return "index";
}
}
浏览器url访问默认是get请求的
属性名 | 描述 |
---|---|
value | 指定请求的实际访问地址,默认@RequestMapping(“url”)的值url即为value的值。指定的地址可以是 URI Template 模式。 |
method | 指定请求的method类型,主要有 GET、POST、DELETE、PUT等; |
params | 指定request中必须包含某些参数值,包含才让该方法处理请求。 |
headers | 指定request中必须包含某些指定的header值,包含才能让该方法处理请求。 |
consumes | 指定处理请求的提交内容类型(Content-Type),例如:application/json, text/html; |
produces | 指定返回的内容类型,当且仅当request请求头中的(Accept)类型中包含该指定类型才返回; |
访问/,/hello,/hi都是访问这个hello方法
@Controller
public class HelloController {
@RequestMapping(value = {"/","/hello","/hi"})
//@RequestMapping(path = {"/","/hello","/hi"})// path也可以
public String hello(){
return "index";
}
}
//@RequestMapping(value = "/hello",method = {RequestMethod.POST,RequestMethod.GET})
@RequestMapping(value = "/hello",method = RequestMethod.POST)
public String hello(){
return "index";
}
指定request中必须包含某些参数值,包含才让该方法处理请求。
//请求中的参数who值为world才执行该方法
//@RequestMapping(value = "/hello",params = {"who=world"})
//请求中的参数who值不为world才执行该方法
@RequestMapping(value = "/hello",params = {"who!=world"})
public String hello(String who){
System.out.println("hello" + who);
return "index";
}
指定request请求作用域中必须包含某些指定的header值,包含才能让该方法处理请求。
@RequestMapping(value = "/hello",headers={"context-type=text/plain","context-type=text/html"})
public String hello(){
return "index";
}
上述访问,如果请求头中不包含context-type=text/plain,context-type=text/html这两个属性,那么就不能访问到该方法,报404错误。
@RequestMapping(value = "/hello", method = RequestMethod.POST, consumes="application/json")
public String hello(){
return "index";
}
方法仅处理request Content-Type为“application/json”类型的请求。
@RequestMapping(value = "/hello", method = RequestMethod.GET, produces="application/json")
@ResponseBody
public String hello(){
return "index";
}
方法仅处理request请求中Accept头中包含了"application/json"的请求,同时暗示了返回的内容类型为application/json;
String realName=request.getParameter(“realName”);
Integer id=request.getParameter(“id”);
@RequestMapping(value = "/hello")
public String hello(String who){
System.out.println("hello" + who);
return "index";
}
此时who这个参数可传可不传,但如果传参参数名必须是who
@RequestMapping(value = "/hello")
public String hello(String who,String count){
System.out.println("hello" + who + ","+ count);
return "index";
}
此时who、count都可传可不传,但如果传参参数名必须是who和count,顺序无所谓
@RequestMapping(value = "/hello")
public String hello(int count){
System.out.println("hello,"+ count);
return "index";
}
@RequestMapping(value = "/hello")
public String hello(@RequestParam String who){
System.out.println("hello" + who);
return "index";
}
required属性,默认值true表示必传,false表示非必传
@RequestMapping(value = "/hello")
public String hello(@RequestParam(required = false) String who){
System.out.println("hello" + who);
return "index";
}
name属性
@RequestMapping(value = "/hello")
public String hello(@RequestParam(name = "paramName",required = false) String who){
System.out.println("hello" + who);
return "index";
}
defaultvalue属性
@RequestMapping(value = "/hello")
public String hello(@RequestParam(defaultValue = "springmvc") String who){
System.out.println("hello" + who);
return "index";
}
通常传对象都是用Post请求或者Put请求
@PostMapping(value = "/hello")
public String hello(User user){
System.out.println("hello:" + user);
return "index";
}
@GetMapping(value = "/hello")
public String hello(String[] hobbys){
for (String hobby : hobbys) {
System.out.println(hobby);
}
return "index";
}
需要加上@RequestParam才行,否则报错
@GetMapping(value = "/hello")
public String hello(@RequestParam List<String> hobbys){
for (String hobby : hobbys) {
System.out.println(hobby);
}
return "index";
}
示例
@GetMapping(value = "/hello")
public String hello(@RequestBody List<User> userList){
userList.forEach(System.out::println);
return "index";
}
配置转换适配器,否则会报错HttpMediaTypeNotSupportedException: Content type 'application/json' not supp
<!--处理器适配器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>
</list>
</property>
</bean>
测试
@GetMapping(value = "/hello")
public ModelAndView hello(){
ModelAndView mv = new ModelAndView();
mv.setViewName("index");//设置返回的逻辑视图名
mv.addObject("msg","hello world");//设置后端向前端传递的数据
return mv;
}
@GetMapping(value = "/hello")
public String hello(Model model){
model.addAttribute("msg","Hello,SpringMVC");
return "index";
}
@GetMapping(value = "/hello")
public String hello(Map<String,Object> returnMap){
returnMap.put("msg","Hello,SpringMVC");
return "index";
}
使用@ResponseBody注解
@GetMapping(value = "/hello")
@ResponseBody
public User hello(){
User user = new User();
user.setUserName("周杰伦");
user.setUserCode("zjl");
return user;
}
配置:添加消息转换器
<mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> </list> </property> </bean> <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> <value>application/json</value> </list> </property> <property name="features"> <list> <!-- Date的日期转换器 --> <value>WriteDateUseDateFormat</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven>
@Controller
@ResponseBody
public class HelloController {
...
}
作用不再解释了,直接看源码,一目了然
method | 操作类型 |
---|---|
GET | 查询 |
POST | 新增 |
PUT | 修改 |
DELETE | 删除 |
package cn.smbms.controller; import cn.smbms.pojo.User; import org.springframework.web.bind.annotation.*; /** * @author: zjl * @datetime: 2024/4/19 * @desc: */ @RestController @RequestMapping("/user") public class UserController { @GetMapping("/{id}") public Integer getById(@PathVariable Integer id) { System.out.println("根据id查询" + id); return id; } @PostMapping("/change") public User insert(@RequestBody User user){ System.out.println("新增用户" + user); return user; } @PutMapping("/change") public User update(@RequestBody User user){ System.out.println("更新用户" + user); return user; } @DeleteMapping("/{id}") public Integer delete(@PathVariable Integer id){ System.out.println("删除用户" + id); return id; } }
http://localhost:9090/smbms/user/1
http://localhost:9090/smbms/user/1
http://localhost:9090/smbms/user/change
http://localhost:9090/smbms/user/change
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。