Spring MVC 获取请求内容(绑定日期对象)、限制请求头
package net.wuxianjie.demo.dto; import java.util.Date; import org.springframework.format.annotation.DateTimeFormat; public class UserDto { private Integer userId; @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") private Date birthday; private String userName; public UserDto() { } public UserDto(Integer userId, Date birthday, String userName) { this.userId = userId; this.birthday = birthday; this.userName = userName; } public Integer getUserId() { return userId; } public void setUserId(Integer userId) { this.userId = userId; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } }
纯文本提交
package net.wuxianjie.demo.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping("/users") public class UserController { @PostMapping("") public @ResponseBody String addUser(@RequestBody String body) { return body; } }
POST http://localhost:8000/users Content-Type: text/plain 提交纯文本内容
表单提交
package net.wuxianjie.demo.controller; import net.wuxianjie.demo.dto.UserDto; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping("/users") public class UserController { @PostMapping("") public @ResponseBody UserDto addUser(UserDto user) { return user; } }
@RequestParam
即可接收请求体参数,与可以接收 URL 参数。
POST http://localhost:8000/users Content-Type: application/x-www-form-urlencoded userId=101&userName=吴仙杰&birthday=2020-06-11 01:09:00
JSON 提交
package net.wuxianjie.demo.controller; import net.wuxianjie.demo.dto.UserDto; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping("/users") public class UserController { @PostMapping("") public @ResponseBody UserDto addUser(@RequestBody UserDto user) { return user; } }
POST http://localhost:8000/users Content-Type: application/json { "user_id": 101, "user_name": "吴仙杰", "birthday": "2020-06-11 01:09:00" }
设置自定义请求头
package net.wuxianjie.demo.controller; import net.wuxianjie.demo.dto.UserDto; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping("/users") public class UserController { @PostMapping(value = "", headers = {"author=wxj", "version=v1"}) public @ResponseBody UserDto addUser(@RequestBody UserDto user) { return user; } }
POST http://localhost:8000/users Content-Type: application/json author: wxj version: v1 { "user_id": 101, "user_name": "吴仙杰", "birthday": "2020-06-11 01:09:00" }
多个 URI 绑定至同一个 Controller 方法
package net.wuxianjie.demo.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/news") public class NewsController { @GetMapping({"", "{c:\\w+}"}) public String getNewsList(@PathVariable(value = "c", required = false) String category) { if (category != null && category.trim().length() > 0) { return "获取【" + category + "】分类的新闻列表"; } return "获取默认新闻列表"; } }
GET http://localhost:8080/news ### GET http://localhost:8080/news/tech
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
上一篇: Web 浏览器自动播放音视频
下一篇: 不要相信一个熬夜的人说的每一句话
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论