开发技巧

200 阅读1分钟

开发技巧

  • get请求包含参数,后台对应类型是Date, 需要在入参上面加一个@DateTimeFormat

    • 前端请求:
    GET http://localhost:8080/query?name=laza&birthday=1978-08-09
    
    • 后台请求函数
     public UserInfo getUser(@DateTimeFormat(pattern="yyyy-MM-dd") LocalDate birthday) {
    
  • post请求包含实体对象

    • UserInfo 日期字段get和set方法上加上 @DateTimeFormat和@JsonFormat

      public class UserInfo implements Serializable {
          @ApiModelProperty(value = "姓名", required = true, example = "沙小卫")
          private String name;
          @ApiModelProperty(value = "年龄", required = true, example = "40")
          private Integer age;
          @ApiModelProperty(value = "出生日期", required = true, example = "1980-01-01")
          private LocalDate birthday;
      
      
          public String getName() {
              return name;
          }
      
          public void setName(String name) {
              this.name = name;
          }
      
          public Integer getAge() {
              return age;
          }
      
          public void setAge(Integer age) {
              this.age = age;
          }
      
          @DateTimeFormat(pattern = "yyyy-MM-dd")
          public LocalDate getBirthday() {
              return birthday;
          }
      
          @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd")
          public void setBirthday(LocalDate birthday) {
              this.birthday = birthday;
          }
      
      }
      
    • post请求体:

      {
        "age": 40,
        "birthday": "1980-01-01",
        "name": "沙小卫"
      }
      

eurekaserver