java更改protobuf的值_protobuf java class 如何添加接口的数据校验?

我计划使用 springboot + protocol buffer + https 完成一个java后台,但是在使用protocol buffer做接口的数据交换的时候遇到了一个问题,似乎没法添加数据校验。以前自己在写JOPO的时候,可以使用hibernate validator进行数据校验。但是protocol buffer作为接口的数据交换格式的时候却没法找到这样的实现技术。我希望找到能够解决protocol buffer添加数据校验的相关技术,或者其他好的解决方法。

使用Hibernate Validator可以这样做数据校验。

import javax.validation.constraints.Max;

import javax.validation.constraints.Min;

import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.Length;

import com.fasterxml.jackson.annotation.JsonIgnore;

import com.fasterxml.jackson.annotation.JsonProperty;

public class User {

private long userId;

@NotNull

@Length(min=5, max=20)

private String name;

@NotNull

private String email;

private String phone;

@Length(min=8, max=16)

private String password;

@Min(1)

@Max(150)

private int age;

public long getUserId() {

return userId;

}

public void setUserId(long userId) {

this.userId = userId;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getEmail() {

return email;

}

public void setEmail(String email) {

this.email = email;

}

public String getPhone() {

return phone;

}

public void setPhone(String phone) {

this.phone = phone;

}

@JsonIgnore

public String getPassword() {

return password;

}

@JsonProperty

public void setPassword(String password) {

this.password = password;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

@Override

public String toString() {

return "User [userId=" + userId + ", name=" + name + ", email=" + email + ", phone=" + phone + ", password="

+ password + ", age=" + age + "]";

}

}

@RestController

@RequestMapping(value="/validators2", consumes="application/json")

@Validated

public class Volidator2Controller {

@PostMapping("")

public Object addUser(@Validated User user) {

return user;

}

}

但是protocol却是没有办法完成这个实现。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值