SpirngMVC Endpoint请求参数如何控制验证

package com.example.demo.controller;

import javax.validation.constraints.NotBlank;

import org.springframework.http.ResponseEntity;

import org.springframework.validation.annotation.Validated;

import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.web.bind.annotation.RequestHeader;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.bind.annotation.RestController;

import com.example.demo.controller.model.PostModel;

@Validated

@RestController

@RequestMapping("/mb/v1")

public class MemberEndpoint {

@RequestMapping(value = "/get", method = RequestMethod.GET)

public ResponseEntity get(

@NotBlank(message = "Name can't be blank.") @RequestParam(name = "name", required = false) String name)

throws Exception {

return ResponseEntity.ok().body("success");

}

@RequestMapping(value = "/header", method = RequestMethod.GET)

public ResponseEntity header(

@NotBlank(message = "TraceId can't be blank.") @RequestHeader(name = "traceId", required = false) String traceId)

throws Exception {

return ResponseEntity.ok().body("success");a

}

@RequestMapping(value = "/getAndHeader", method = RequestMethod.GET)

public ResponseEntity getAndHeader(

@NotBlank(message = "Name can't be blank.") @RequestParam(name = "name", required = false) String name,

@NotBlank(message = "TraceId can't be blank.") @RequestHeader(name = "traceId", required = false) String traceId) {

return ResponseEntity.ok().body("success");

}

@RequestMapping(value = "/post", method = RequestMethod.POST, consumes = "application/json;charset=UTF-8")

public ResponseEntity post(@Validated @RequestBody PostModel model) {

return ResponseEntity.ok().body("success");

}

@RequestMapping(value = "/postAndHeader", method = RequestMethod.POST, consumes = "application/json;charset=UTF-8")

public ResponseEntity postAndHeader(

@NotBlank(message = "TraceId can't be blank.") @RequestHeader(name = "traceId", required = false) String traceId,

@Validated @RequestBody PostModel model) {

return ResponseEntity.ok().body("success");

}

@RequestMapping(value = "/postAndGetAndHeader", method = RequestMethod.POST, consumes = "application/json;charset=UTF-8")

public ResponseEntity postAndGetAndHeader(

@NotBlank(message = "Name can't be blank.") @RequestParam(name = "name", required = false) String name,

@NotBlank(message = "TraceId can't be blank.") @RequestHeader(name = "traceId", required = false) String traceId,

@Validated @RequestBody PostModel model) {

return ResponseEntity.ok().body("success");

}

}



/////////////////

package com.example.demo.controller;

import java.util.List;

import java.util.Set;

import java.util.stream.Collectors;

import javax.validation.ConstraintViolation;

import javax.validation.ConstraintViolationException;

import org.springframework.http.ResponseEntity;

import org.springframework.validation.ObjectError;

import org.springframework.web.bind.MethodArgumentNotValidException;

import org.springframework.web.bind.annotation.ExceptionHandler;

import org.springframework.web.bind.annotation.ResponseBody;

import org.springframework.web.bind.annotation.RestControllerAdvice;

import com.example.demo.controller.model.ResponseResult;

@RestControllerAdvice

public class EndpointAdvice {

@ExceptionHandler(MethodArgumentNotValidException.class)

@ResponseBody

public ResponseEntity methodArgumentExceptionHandle(MethodArgumentNotValidException e) {

boolean hasErrors = e.getBindingResult().hasErrors();

if (hasErrors) {

List errors = e.getBindingResult().getAllErrors();

List errorList = errors.stream().map(r -> r.getDefaultMessage()).collect(Collectors.toList());

String message = String.join(";", errorList);

return ResponseEntity.ok().body(new ResponseResult("-1", message));

}

return ResponseEntity.ok().body(new ResponseResult("0", null));

}

@ExceptionHandler(ConstraintViolationException.class)

@ResponseBody

public ResponseEntity constraaintViolationExceptionHandle(ConstraintViolationException e) {

Set> set = e.getConstraintViolations();

List errorList = set.stream().map(r -> r.getMessage()).collect(Collectors.toList());

if (!errorList.isEmpty()) {

String message = String.join(";", errorList);

return ResponseEntity.ok().body(new ResponseResult("-1", message));

}

return ResponseEntity.ok().body(new ResponseResult("0", null));

}

}

你可能感兴趣的:(SpirngMVC Endpoint请求参数如何控制验证)