Spring boot 與RestEasy結合
Spring Boot 官方並沒有出於RestEasy結合的jar包,估計是應爲RestEasy用的比較少吧,但是paypal團隊出了一個jar(傳送門1),估計他們那邊有RestEasy用的比較多吧,PayPal是國際版本的支付寶(傳送門2)
首先導包
<dependency>
<groupId>com.paypal.springboot</groupId>
<artifactId>resteasy-spring-boot-starter</artifactId>
<version>2.3.3-RELEASE</version>
<scope>runtime</scope>
</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包
<div class="se-preview-section-delimiter"></div>
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<ResteasyConstraintViolation> 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<UserVO> add(@Valid UserParam userParam)
{
return userService.add(userParam);
}
“`