Spring Boot 官方并没有出于RestEasy结合的jar包,估计是应为RestEasy用的比较少吧,但是paypal团队出了一个jar(传送门1),估计他们那边有RestEasy用的比较多吧,PayPal是国际版本的支付宝(传送门2)
首先导包
<dependency>
<groupId>com.paypal.springbootgroupId>
<artifactId>resteasy-spring-boot-starterartifactId>
<version>2.3.3-RELEASEversion>
<scope>runtimescope>
dependency>
然后定义一个JAX-RS格式的应用类(application 继承javax.ws.rs.core.Application)
然后作为Spring的bean来注册就好了
package com.sample.app;
import org.springframework.stereotype.Component;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@Component
@ApplicationPath("/sample-app/")
public class JaxrsApplication extends Application {
}
````
RestEasy 的resources 和providers 只要声明为Spring 的Bean,它就是会自动注册
然后在Spring boot的配置文件中配置Application.property文件里面设置 resteasy.jaxrs.app.registration
让他是否自动注册,取值有
1. beans
1. property
1. scanning
1. auto (default)
更多信息,[传送门](https://github.com/paypal/resteasy-spring-boot/blob/master/mds/USAGE.md)
ReatEasy 校验参数,全局处理返回结果
先添加jar包
class="se-preview-section-delimiter">
org.jboss.resteasy
resteasy-validator-provider-11
3.1.4.Final
这个包主要是将hibernate-validator和RestEasy全局异常处理接口ExceptionMapping结合使用
首先实现全局validator异常处理
<div class="se-preview-section-delimiter">div>
package com.hey900.oa.filter;
import com.hey900.oa.Result;
import org.jboss.resteasy.api.validation.ResteasyConstraintViolation;
import org.jboss.resteasy.api.validation.ResteasyViolationException;
import org.springframework.stereotype.Component;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
import java.util.List;
/**
* Created with IntelliJ IDEA.
* Description:
* UserVO:刘敏华 [email protected]
* DateDeserializer: 2017-08-08
* Time: 13:44
*/
@Component
@Provider
public class ParamMapping implements ExceptionMapper {
@Override
public Response toResponse(ResteasyViolationException restEasyViolationException ) {
List violations = restEasyViolationException.getViolations();
ResteasyConstraintViolation resteasyConstraintViolation = null;
if(violations!=null&&!violations.isEmpty()){
resteasyConstraintViolation = violations.get(0);
}
return Response.ok(Result.fail(resteasyConstraintViolation.getMessage()), MediaType.APPLICATION_JSON_TYPE).build();
}
}
然后使用
<div class="se-preview-section-delimiter">div>
/**
* Created with IntelliJ IDEA.
* Description:
* UserVO:刘敏华 [email protected]
* DateDeserializer: 2017-08-08
* Time: 14:18
*/
public class UserParam {
@NotNull(message = "用户名不能为空")
private String name;
@Pattern(regexp = "^[a-zA-Z0-9_.-][email protected]",message = "请输入正确的公司邮箱")
@ApiModelProperty("员工邮箱")
private String email;
@NotNull(message = "手机号不能为空")
@Size(min = 11,max=11,message = "请填写合法的手机号")
private String phone;
@NotNull(message = "登录密码不能为空")
@Size(min = 6,max=20,message = "密码长度需要再6~20位字符之间")
private String password;
@NotNull(message = "入职日期不能为空")
@JsonDeserialize(using = DateDeserializer.class)
private Date entryDate;
@NotNull(message = "转正日期不能为空")
@JsonDeserialize(using = DateDeserializer.class)
private Date regularDate;
@JsonDeserialize(using = DateDeserializer.class)
private Date leaveDate;
在Api里面校验
<div class="se-preview-section-delimiter">div>
@Secured(resource = "user.add",name = "新增用户")
@POST
@Path("/add")
@ApiOperation("用户增加接口")
public Result add(@Valid UserParam userParam)
{
return userService.add(userParam);
}
“`