Spring MVC 获取请求内容(绑定日期对象)、限制请求头

发布于 2025-01-23 17:16:28 字数 4580 浏览 0 评论 0

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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

关于作者

做个ˇ局外人

暂无简介

文章
评论
25 人气
更多

推荐作者

白云不回头

文章 0 评论 0

糖粟与秋泊

文章 0 评论 0

洋豆豆

文章 0 评论 0

泛滥成性

文章 0 评论 0

mb_2YvjCLvt

文章 0 评论 0

夜光

文章 0 评论 0

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文