接上篇文章https://blog.csdn.net/supreme000/article/details/109049545
完善service参数校验
service-api模块依赖
<dependencies>
<!-- 参数校验相关依赖 -->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId> <!-- JSR 参数校验规范 API -->
<version>2.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId> <!-- JSR 参数校验规范实现,我们使用 hibernate-validator -->
<version>6.0.18.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator-cdi</artifactId>
<version>6.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.el</artifactId> <!-- 可能涉及到 EL 表达,所以引入,否则 hibernate-validator 在初始化会报错 -->
<version>3.0.1-b11</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
<scope>compile</scope>
</dependency>
</dependencies>
使用JSR303参数校验规范,这个与SpringBoot的controller层的参数校验类似
UserDTO参数增加校验注解
package com.dubbo.api.dto;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
public class UserDTO implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 用户编号
*/
@NotNull(message = "用户编号不能为空")
private Integer id;
/**
* 姓名
*/
@NotEmpty(message = "姓名不能为空")
@Size(min = 5, max = 10, message = "姓名长度范围是5到10")
private String name;
/**
* 性别
*/
@NotNull(message = "性别不能为空")
private Integer gender;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
@Override
public String toString() {
return "UserDTO{" +
"id=" + id +
", name='" + name + '\'' +
", gender=" + gender +
'}';
}
}
增加一个UserAddDTO
package com.dubbo.api.dto;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
public class UserAddDTO implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 姓名
*/
@NotEmpty(message = "姓名不能为空")
@Size(min = 5, max = 10, message = "姓名长度范围是5到10")
private String name;
/**
* 性别
*/
@NotNull(message = "性别不能为空")
private Integer gender;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
@Override
public String toString() {
return "UserAddDTO{" +
"name='" + name + '\'' +
", gender=" + gender +
'}';
}
}
UserService接口的getUser方法的id参数增加校验
package com.dubbo.api.service;
import com.dubbo.api.dto.UserAddDTO;
import com.dubbo.api.dto.UserDTO;
import javax.validation.constraints.NotNull;
public interface UserService {
/**
* 根据用户id获取用户信息
* @param id 用户id
* @return
*/
UserDTO getUser(@NotNull(message = "用户id不能为空") Integer id);
Integer addUser(UserAddDTO userAddDTO);
}
生产者和消费者的dubbo.xml的配置中<dubbo:service>与<dubbo:reference>标签的validation属性都设置为true,开启参数校验
如果服务消费者开启参数校验,请求参数校验不通过时,结束请求,抛出 ConstraintViolationException 异常。不会向服务提供者发起请求。
如果服务提供者开启参数校验,请求参数校验不通过时,结束请求,抛出 ConstraintViolationException 异常。不会执行后续的业务逻辑。
消费者在run方法中进行测试
package com.dubbo.consumer.test;
import com.dubbo.api.dto.UserAddDTO;
import com.dubbo.api.dto.UserDTO;
import com.dubbo.api.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component
public class UserServiceRPCTest implements CommandLineRunner {
private static final Logger log = LoggerFactory.getLogger(UserServiceRPCTest.class);
@Resource
private UserService userService;
@Override
public void run(String... args) throws Exception {
// for(int i = 0; i < 100; i++){
// UserDTO userDTO = userService.getUser(i);
// log.info("服务消费者发起RPC调用,返回结果是{}", userDTO.toString());
// }
// userService.getUser(null);
UserAddDTO userAddDTO1 = new UserAddDTO();
userAddDTO1.setName("testesttestest");
userAddDTO1.setGender(1);
userService.addUser(userAddDTO1);
UserAddDTO userAddDTO2 = new UserAddDTO();
userAddDTO2.setGender(null);
userService.addUser(userAddDTO2);
}
}
运行结果