具有错误查询参数的Java Rest Web服务
我有下面的web服务方法具有错误查询参数的Java Rest Web服务,java,web-services,rest,Java,Web Services,Rest,我有下面的web服务方法 @GET @Produces({ "application/xml", "application/json" }) @Path("/service/salesorder/{identifier}/{orderType}") public Response readOrder(@PathParam("identifier") String identifier, @PathParam("orderType") Strin
@GET
@Produces({ "application/xml", "application/json" })
@Path("/service/salesorder/{identifier}/{orderType}")
public Response readOrder(@PathParam("identifier") String identifier,
@PathParam("orderType") String orderType);
如果用户调用下面正确的url,我希望服务正常工作,如果任何一个调用错误的url(额外的查询参数),这在方法中没有定义,我想抛出一些错误,我如何捕获所有错误的url
正确的URL:
错误URL:此外,您应该插入@Context方法参数:
public Response readOrder(@PathParam("identifier") String identifier,
@PathParam("orderType") String orderType,
@Context HttpServletRequest req);
然后使用servlet API HttpServletRequest#getParameterNames()检查是否提供了任何查询参数。您可以使用不同的
@PathParam
值
@Path("/service/salesorder/{identifier}/{orderType:[^&]*}")
这将导致
/service/salesforder/service/salesforder/foo/bar&baz
sind{orderType:[^&]*}
与ShoppingOrder&badparam
不匹配,即/service/salesforder/123/ShoppingOrder&badparam
不是有效路径。它的HTTP状态是什么?