spring 注释快捷键_Spring 处理请求和响应相关的注解

java

java8

java开发

Spring 处理请求和响应相关的注解

@Controller

默认返回 templates 目录下的 string.html 页面内容。

在方法中加上 @ResponseBody 注解,可以返回JSON、XML或自定义mediaType的内容

@RestController

直接返回内容,会自动将对象实体转换为JSON格式,视图解析器 InternalResourceViewResolver 不起作用。

@RestController = @Controller + @ResponseBody

@RequestBody

接收请求体中的 JSON 数据,通过实体类的setter方法赋值给属性。

json 的 "" => 实体 String 为 ""

json 的 "" => 实体 Integer、Double 为 null

json 的 null => 实体为 null

@RequestBody 可以与 @RequestParam() 同时使用,@RequestBody 最多只能有一个,@RequestParam() 可以有多个。

以 String 接收数据

@RequestMapping("/index")

public String indexMapping(@RequestBody String jsonStr) {

return jsonStr;

}

以对象实体接收数据

// {"name":"hanmeimei","age":12}

@RequestMapping("/index")

public String indexMapping(@RequestBody User user) {

return user.toString();

}

以复杂的对象实体接收数据

public class Team {

private Integer id;

private String name;

private List honors;

private List members;

}

// {

// "id": 1,

// "name": "good",

// "honors": ["very good", "very fast"],

// "members": [{"name":"hanmeimei","age":12},

// {"name":"lilei","age":13}],

// }

@RequestMapping("/index")

public String indexMapping(@RequestBody Team team) {

return team.toString();

}

@ResponseBody

将对象实体转换为JSON、XML或自定义mediaType的内容,并在 HTTP response body 中返回

@RequestMapping

将请求映射到控制器上,可以在控制器类和/或方法上使用。

处理单个请求

@RequestMapping("/home")

public class IndexController {

@RequestMapping("/index")

String indexMapping() {

return "Hello from index mapping.";

}

}

处理多个请求

@RequestMapping("/home")

public class IndexController {

@RequestMapping(value = {

"/",

"/index",

"/index/*.html",

"/index/**/*.html"

})

String indexMultipleMapping() {

return "Hello from index multiple mapping.";

}

}

处理请求类型

默认是 HTTP GET 类型的。

@RequestMapping(value = "/home", method = RequestMethod.GET)

String get() {}

@RequestMapping(value = "/home", method = RequestMethod.DELETE)

String delete() {}

@RequestMapping(value = "/home", method = RequestMethod.POST)

String post() {}

@RequestMapping(value = "/home", method = RequestMethod.PUT)

String put() {}

@RequestMapping(value = "/home", method = RequestMethod.PATCH)

String patch() {}

处理请求类型快捷方式

@GetMapping(value = "/home")

String get() {}

@DeleteMapping(value = "/home")

String delete() {}

@PostMapping(value = "/home")

String post() {}

@PutMapping(value = "/home")

String put() {}

@PatchMapping(value = "/home")

String patch() {}

处理生产和消费对象

public class IndexController {

// 生产 application/JSON 响应

@RequestMapping(value = "/prod", produces = {

"application/JSON"

})

@ResponseBody

String getProduces() {

return "Produces attribute";

}

// 消费 application/JSON & application/XML 请求

@RequestMapping(value = "/cons", consumes = {

"application/JSON",

"application/XML"

})

@ResponseBody

String getConsumes() {

return "Consumes attribute";

}

}

处理消息头

public class IndexController {

// 处理 Content-Type=application/json 的请求

@RequestMapping(value = "/head", headers = {

"Content-Type=application/json"

})

String head() {

return "Mapping applied along with headers";

}

}

public class IndexController {

@RequestMapping(value = "/head", headers = {

"Content-Type=text/plain",

"Content-Type=application/json"

})

String head() {

return "Mapping applied along with headers";

}

}

处理请求参数

public class IndexController {

@RequestMapping(value = "/fetch", params = {

"personId=10"

})

String getParams10(@RequestParam("personId") String id) {

return "Fetched parameter using params attribute = " + id;

}

@RequestMapping(value = "/fetch", params = {

"personId=20"

})

String getParams20(@RequestParam("personId") String id) {

return "Fetched parameter using params attribute = " + id;

}

}

处理动态 URI

public class IndexController {

@RequestMapping(value = "/fetch/{id}")

String getDynamicUriValue(@PathVariable String id) {

return "Dynamic URI parameter fetched";

}

@RequestMapping(value = "/fetch/{id:d+}/{name}")

String getDynamicUriValueRegex(

@PathVariable("id") int id, @PathVariable("name") String name

) {

return "Dynamic URI parameter fetched using regex";

}

}

默认的处理方法

public class IndexController {

@RequestMapping()

String default () {

return "This is a default method for the class";

}

}

内容来源于网络,如有侵权请联系客服删除

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值