繁体   English   中英

如何处理CXF restful webservices中的可选参数

[英]How to handle optional parameters in CXF restful webservices

我已经按照此链接构建了CXF Restful webservices url链接

如果假设我的网址如下所述:

http://localhost:8080/CxfRestService/rest/employeeservices/getemployeedetail?employeeId=1&empProfession=software

这里,“empProfession”参数对我来说是可选的。

所以,尽管如果我省略了该参数并点击下面的url,我应该得到所需的响应。 http://localhost:8080/CxfRestService/rest/employeeservices/getemployeedetail?employeeId=1

任何人都可以帮我解决如何在CXF Restful Web服务中使用可选参数。

选项1 - 声明参数并检查是否!= null

 public Response getEmployeeDetail(@QueryParam("employeeId") String employeeId, @QueryParam("empProfession") String empProfession);

选项2 - 声明en对象以接收所有已知参数

 public Response getEmployeeDetail(@QueryParam("") EmployeeFilter filter) ;

 public class EmployeeFilter {
    public void setEmployeeId(String id) {...}
    public void setEmpProfession(String p) {...}  
 }

选项3 - 不要声明参数并解析URI。 如果您可以接受非固定参数,则此选项可能很有用

 public Response getEmployeeDetail( @Context UriInfo uriInfo) {
      MultivaluedMap<String, String> params = uriInfo.getQueryParameters();
      String employeeId = params.getFirst("employeeId");
      String empProfession = params.getFirst("empProfession");

事实上,CXF中的所有参数都不是强制性的,您无法使用@QueryParam更改此参数(因为您可以使用@RequestParam(required = false)与Spring-REST进行比较)。

解决方案是添加@NotNull javax.validation批注以指示参数是必需的。

这样,你就可以使用了

  • CXF3 ValidationFeature使用@Valid自动验证它。
  • CXF3 SwaggerFeature还将在API文档中呈现必需参数
  • CXF2手动执行bean验证

有关使用javax.validation注释的更多信息,请参阅CXF3 ValidationFeature: https ://cwiki.apache.org/confluence/display/CXF20DOC/ValidationFeature

有关CXF3 Swagger功能的更多信息,请访问: http ://cxf.apache.org/docs/swagger2feature.html)。

这个答案是相关的: JAX-RS中必需的@QueryParam(以及在缺席时要做什么)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM