一.创建idea
二.向pom.xml添加依赖
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.7.RELEASE</version></parent><properties><java.version>1.8</java.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><spring-boot.version>2.6.13</spring-boot.version><jwt.version>0.7.0</jwt.version><fastjson.version>1.2.60</fastjson.version><spring-cloud-alibaba.version>2.0.3.RELEASE</spring-cloud-alibaba.version><spring-cloud.version>Finchley.SR2</spring-cloud.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><!--json--><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>${fastjson.version}</version><scope>compile</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>${spring-cloud-alibaba.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><classifier>exec</classifier></configuration></plugin></plugins></build>
三.添加application.yml
server:port: 100
四.创建实体类
package com.jr.entry;import lombok.Data;
import org.hibernate.validator.constraints.Length;
import org.springframework.stereotype.Component;import javax.validation.constraints.NotEmpty;@Component
@Data
public class User {private String id;// @NotNull name为空地址@NotEmpty //name为空值,空双引private String name;@Length(min = 6,max = 10)private String password;}
五. 创建控制类
package com.jr.controller;import com.jr.entry.User;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;@RestController
@RequestMapping("/demo")
public class DemoController {@PostMappingpublic String testJsr303(@Validated @RequestBody User user){return "ok";}
}
六.创建启动类
package com;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class SpringBootMain {public static void main(String[] args) {SpringApplication.run(SpringBootMain.class,args);}
}
1.使用@PostMapping注解时,接口要想成功接受到数据,需要使用x-www-form-urlencoded数据的格式发送数据。
2.使用@PostMapping+@RequestBody注解时,接口要想成功接受到数据需要使用raw数据的格式发送数据。
3.但是使用html静态页面发送post请求时,数据是按照application/x-www-form-urlencoded;charset=UTF-8格式发送的,格式不符合,数据接收不到。
启动启动器
七.使用postman工具
因为前端页面无法模拟,前端页面在这里创建不了,所以我们用postman工具演示。填好上面的地址:http://localhost:100/demo,点击body的raw输入代码后,点击send发送。
八.规范返回值格式
但是发现响应的值,虽然是json格式的。但是这个格式并不是咱们想要的,前台后台都是约定好的数据格式,如果你的数据格式发生了变化,那么就需要特殊处理了,很麻烦。所以我们要对响应的结果做一个处理。每个项目组都不一样,只要会怎么用就行。
8.1添加ResultCode工具类
public enum ResultCode {SUCCESS(0, "请求成功"),ERROR(1, "请求失败"),;private int code;private String message;ResultCode(int code, String message) {this.code = code;this.message = message;}public int getCode() {return code;}public String getMessage() {return message;}
}
8.2添加Result类
package com.jr.util;import lombok.Data;
import java.util.HashMap;
import java.util.Map;@Data
public class Result {private Integer code;private String message;private Map<String, Object> map = new HashMap<>();private Result() {}public static Result ok() {Result r = new Result();r.setCode(ResultCode.SUCCESS.getCode());r.setMessage(ResultCode.SUCCESS.getMessage());return r;}public static Result error() {Result r = new Result();r.setCode(ResultCode.ERROR.getCode());r.setMessage(ResultCode.ERROR.getMessage());return r;}public Result put(String key, Object value) {map.put(key, value);return this;}public Object get(String key) {return map.get(key);}}
8.3添加ValidationExceptionAdvice工具类
package com.jr.util;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;import java.util.HashMap;
import java.util.List;
import java.util.Map;@RestControllerAdvice("com.jr.controller")
public class ValidationExceptionAdvice {@ExceptionHandler(MethodArgumentNotValidException.class)public Result handlerMethodArgumentNotValidException(MethodArgumentNotValidException e) {BindingResult bindingResult = e.getBindingResult();List<FieldError> fieldErrors = bindingResult.getFieldErrors();Map<String, Object> map = new HashMap<>();for (FieldError fieldError : fieldErrors) {map.put(fieldError.getField(), fieldError.getDefaultMessage());}return Result.error().put("data", map);}
}
重新启动项目
九.自己的idea项目结构
今天就到此位置,希望能帮到大家