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
@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
@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
@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
return ResponseEntity.ok().body("success");
}
@RequestMapping(value = "/postAndHeader", method = RequestMethod.POST, consumes = "application/json;charset=UTF-8")
public ResponseEntity
@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
@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
boolean hasErrors = e.getBindingResult().hasErrors();
if (hasErrors) {
List
List
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
Set
List
if (!errorList.isEmpty()) {
String message = String.join(";", errorList);
return ResponseEntity.ok().body(new ResponseResult("-1", message));
}
return ResponseEntity.ok().body(new ResponseResult("0", null));
}
}