• 005 使用SpringMVC开发restful API三--处理创建请求


    一:主要任务

    1.说明

      @RequestBody 映射请求体到java方法的参数

      日期类型参数的处理

      @Valid注解

      BindingResult验证请求参数的合法性并处理校验结果

    二:@RequestBody

    1.@RequestBody

      测试类

     1 /**
     2      * @throws Exception 
     3      * 
     4      */
     5     @Test
     6     public void whenCreateSuccess() throws Exception {
     7         String content="{"username":"tom","password":null}";
     8         mockMvc.perform(MockMvcRequestBuilders.post("/user")
     9                 .contentType(MediaType.APPLICATION_JSON_UTF8)
    10                 .content(content))
    11             .andExpect(MockMvcResultMatchers.status().isOk())
    12             .andExpect(MockMvcResultMatchers.jsonPath("$.id").value("1"));
    13     }

      User.java

     1 package com.cao.dto;
     2 
     3 import com.fasterxml.jackson.annotation.JsonView;
     4 
     5 public class User {
     6     //接口
     7     public interface UserSimpleView {};
     8     public interface UserDetailView extends UserSimpleView {};    //继承之后,可以展示父的所有
     9     
    10     private String username;
    11     private String password;
    12     private String id;
    13     
    14     @JsonView(UserSimpleView.class)
    15     public String getUsername() {
    16         return username;
    17     }
    18     public void setUsername(String username) {
    19         this.username = username;
    20     }
    21     
    22     @JsonView(UserDetailView.class)
    23     public String getPassword() {
    24         return password;
    25     }
    26     public void setPassword(String password) {
    27         this.password = password;
    28     }
    29     
    30     @JsonView(UserSimpleView.class)
    31     public String getId() {
    32         return id;
    33     }
    34     public void setId(String id) {
    35         this.id = id;
    36     }
    37     
    38 }

      控制类

     1     @JsonView(User.UserDetailView.class)
     2     @PostMapping
     3     public User create(@RequestBody User user){
     4         System.out.println(user.getId());
     5         System.out.println(user.getUsername());
     6         System.out.println(user.getPassword());
     7         
     8         user.setId("1");
     9         return user;
    10     }

    三:日期类型的处理

    1.说明

      使用时间戳。

    2.程序

      User中添加Date类型字段

     1 package com.cao.dto;
     2 
     3 import java.util.Date;
     4 
     5 import com.fasterxml.jackson.annotation.JsonView;
     6 
     7 public class User {
     8     //接口
     9     public interface UserSimpleView {};
    10     public interface UserDetailView extends UserSimpleView {};    //继承之后,可以展示父的所有
    11     
    12     private String username;
    13     private String password;
    14     private String id;
    15     private Date birthday;
    16     
    17     @JsonView(UserSimpleView.class)
    18     public String getUsername() {
    19         return username;
    20     }
    21     public void setUsername(String username) {
    22         this.username = username;
    23     }
    24     
    25     @JsonView(UserDetailView.class)
    26     public String getPassword() {
    27         return password;
    28     }
    29     public void setPassword(String password) {
    30         this.password = password;
    31     }
    32     
    33     @JsonView(UserSimpleView.class)
    34     public String getId() {
    35         return id;
    36     }
    37     public void setId(String id) {
    38         this.id = id;
    39     }
    40     
    41     @JsonView(UserSimpleView.class)
    42     public Date getBirthday() {
    43         return birthday;
    44     }
    45     public void setBirthday(Date birthday) {
    46         this.birthday = birthday;
    47     }
    48         
    49 }

      测试类

     1     /**
     2      * @throws Exception 
     3      * 测试请求的创建
     4      */
     5     @Test
     6     public void whenCreateSuccess() throws Exception {
     7         Date date=new Date();
     8         System.out.println(date.getTime());
     9         String content="{"username":"tom","password":null,"birthday":"+date.getTime()+"}";
    10         String result=mockMvc.perform(MockMvcRequestBuilders.post("/user")
    11                 .contentType(MediaType.APPLICATION_JSON_UTF8)
    12                 .content(content))
    13             .andExpect(MockMvcResultMatchers.status().isOk())
    14             .andExpect(MockMvcResultMatchers.jsonPath("$.id").value("1"))
    15             .andReturn().getResponse().getContentAsString();
    16         System.out.println("result="+result);
    17     }

      控制类

     1 @PostMapping
     2     public User create(@RequestBody User user){
     3         System.out.println(user.getId());
     4         System.out.println(user.getUsername());
     5         System.out.println(user.getPassword());
     6         System.out.println(user.getBirthday());
     7         
     8         user.setId("1");
     9         return user;
    10     }

      效果:

      

    四:@Valid

    1.说明

      只有满足校验的时候,才进行业务处理

    2.校验的步骤

      对字段进行校验要求

      在服务上加@Valid

    3.程序

      User.java

     1 package com.cao.dto;
     2 
     3 import java.util.Date;
     4 
     5 import org.hibernate.validator.constraints.NotBlank;
     6 
     7 import com.fasterxml.jackson.annotation.JsonView;
     8 
     9 public class User {
    10     //接口
    11     public interface UserSimpleView {};
    12     public interface UserDetailView extends UserSimpleView {};    //继承之后,可以展示父的所有
    13     
    14     private String username;
    15     
    16     @NotBlank
    17     private String password;
    18     private String id;
    19     private Date birthday;
    20     
    21     @JsonView(UserSimpleView.class)
    22     public String getUsername() {
    23         return username;
    24     }
    25     public void setUsername(String username) {
    26         this.username = username;
    27     }
    28     
    29     @JsonView(UserDetailView.class)
    30     public String getPassword() {
    31         return password;
    32     }
    33     public void setPassword(String password) {
    34         this.password = password;
    35     }
    36     
    37     @JsonView(UserSimpleView.class)
    38     public String getId() {
    39         return id;
    40     }
    41     public void setId(String id) {
    42         this.id = id;
    43     }
    44     
    45     @JsonView(UserSimpleView.class)
    46     public Date getBirthday() {
    47         return birthday;
    48     }
    49     public void setBirthday(Date birthday) {
    50         this.birthday = birthday;
    51     }
    52         
    53 }

      控制类

     1 @PostMapping
     2     public User create(@Valid @RequestBody User user){
     3         System.out.println(user.getId());
     4         System.out.println(user.getUsername());
     5         System.out.println(user.getPassword());
     6         System.out.println(user.getBirthday());
     7         
     8         user.setId("1");
     9         return user;
    10     }

      测试类

     1 /**
     2      * @throws Exception 
     3      * 测试请求的创建
     4      */
     5     @Test
     6     public void whenCreateSuccess() throws Exception {
     7         Date date=new Date();
     8         System.out.println(date.getTime());
     9         String content="{"username":"tom","password":null,"birthday":"+date.getTime()+"}";
    10         String result=mockMvc.perform(MockMvcRequestBuilders.post("/user")
    11                 .contentType(MediaType.APPLICATION_JSON_UTF8)
    12                 .content(content))
    13             .andExpect(MockMvcResultMatchers.status().isOk())
    14             .andExpect(MockMvcResultMatchers.jsonPath("$.id").value("1"))
    15             .andReturn().getResponse().getContentAsString();
    16         System.out.println("result="+result);
    17     }

    4.效果

      

    5.BindingResult

      在上面的程序中,校验没有通过,则请求不进入方法体,但是有时候还需要一些处理的

    6.程序

      控制类

     1 @PostMapping
     2     public User create(@Valid @RequestBody User user,BindingResult errors){
     3         if(errors.hasErrors()) {
     4             errors.getAllErrors().stream().forEach(error->System.out.println(error.getDefaultMessage()));
     5         }
     6         
     7         System.out.println(user.getId());
     8         System.out.println(user.getUsername());
     9         System.out.println(user.getPassword());
    10         System.out.println(user.getBirthday());
    11         
    12         user.setId("1");
    13         return user;
    14     }

      控制台

      

      这里暂时都打印出来,个性化还需要在后面处理。

  • 相关阅读:
    看懂Oracle执行计划
    pl/sql编译存储过程卡住的解决方法
    ORA-00604的解决方法
    ORACLE SYNONYM详解
    Oracle 用户权限管理方法
    “ORA-06550: 第 1 行, 第 7 列”解决方法
    oracle 存储过程给另一个用户的权限问题
    Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.3:compile
    eclipse maven maven-compiler-plugin 报错 完全解决
    如何用git把本地代码上传到github
  • 原文地址:https://www.cnblogs.com/juncaoit/p/9704247.html
Copyright © 2020-2023  润新知