The user uses the parameter validation on the provider client, and can set the corresponding parameter input requirements in advance, and perform the effect processing before the interface is actually called to achieve the effect of the control parameter input standard.
Add the pom dependency of swagger-invocation-validator:
<dependency> <groupId>org.apache.servicecomb</groupId> <artifactId>swagger-invocation-validator</artifactId> </dependency>
Add validator annotations to the code that requires validation according to the JSR 349 specification, such as @NotNull, @Min, @Max, etc.
@RestSchema(schemaId = "validator") @Path("/validator") @Produces(MediaType.APPLICATION_JSON) public class Validator { @Path("/add") @POST public int add(@FormParam("a") int a, @Min(20) @FormParam("b") int b) { return a + b; } @Path("/sayhi/{name}") @PUT public String sayHi(@Length(min = 3) @PathParam("name") String name) { ContextUtils.getInvocationContext().setStatus(202); return name + " sayhi"; } @Path("/sayhello") @POST public Student sayHello(@Valid Student student) { student.setName("hello " + student.getName()); student.setAge(student.getAge()); return student; } }
You need to add @Valid in front of the incoming Student object, as shown in the figure above, sayHello(@Valid Student student).
public class Student { @NotNull private String name; @Max(20) private int age; public void setName(String name) { this.name = name; } public String getName() { return this.name; } public void setAge(int age) { this.age = age; } public int getAge() { return age; } }
If any parameter validation fails, the default error is BAD_REQUEST(400, “Bad Request”).
Return error support for custom extensions, using the SPI mechanism.
1. Implement the ExceptionToResponseConverter interface, override the method, where the return result of the getOrder method indicates the priority of the validator. The smaller the value, the higher the priority.
```java public class ConstraintViolationExceptionToResponseConverter implements ExceptionToResponseConverter<ConstraintViolationException> { @Override public Class<ConstraintViolationException> getExceptionClass() { return ConstraintViolationException.class; } @Override public Response convert(SwaggerInvocation swaggerInvocation, ConstraintViolationException e) { return Response.createFail(new InvocationException(Status.BAD_REQUEST, e.getConstraintViolations().toString())); } @Override public int getOrder() { return -100; } } ```