1

使用 CXF / JAX-RS 在 GET 请求中检索大量查询参数的最佳方法是什么?显然,拥有很长的@QueryParam注释列表并不理想。我看着MultiValuedMap像这样单独使用:

@GET
@Path("/")
public Response getProjects(MultivaluedMap<String, String> queryParams);

但这导致抛出异常,出于某种原因,CFX 试图处理多部分表单,所以我猜MultivaluedMap只是表单提交,如此处所述:http: //cxf.apache.org/docs/jax-rs-data -bindings.html#JAX-RSDataBindings-Formpayloads。让我感到困惑的是,我猜这是一个数据检索用户案例,所以我将它映射到一个 GET 服务(并在客户端实现了一个 GET 请求),但另一方面,我正在通过表单提交搜索参数,那么处理这个问题的正确方法是什么?如何检索我的 20++ 搜索参数?

这是一个示例请求:

GET http://localhost:8080/myapp-ws/services/setup/projects?  brandId=&_search=false&nd=1372435138080&rows=10&page=1&sidx=projectCode&sord=desc&_=1372435    119675 HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:21.0) Gecko/20100101 Firefox/21.0
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
X-Requested-With: XMLHttpRequest
Referer: http://localhost:8080/myapp-ui/projects.do
Connection: keep-alive

示例响应:

HTTP/1.1 400 Bad Request
Server: Apache-Coyote/1.1
Date: Fri, 28 Jun 2013 15:58:58 GMT
Content-Length: 0
Connection: close

使用 MultivaluedMap 实现的 CXF 服务引发的异常:

Jun 28, 2013 11:58:58 AM org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper toResponse
WARNING: javax.ws.rs.BadRequestException: java.lang.NullPointerException
at org.apache.cxf.jaxrs.provider.FormEncodingProvider.readFrom(FormEncodingProvider.java:118)
at org.apache.cxf.jaxrs.utils.JAXRSUtils.readFromMessageBodyReader(JAXRSUtils.java:1189)
at org.apache.cxf.jaxrs.utils.JAXRSUtils.readFromMessageBody(JAXRSUtils.java:1137)
at org.apache.cxf.jaxrs.utils.JAXRSUtils.processParameter(JAXRSUtils.java:686)
at org.apache.cxf.jaxrs.utils.JAXRSUtils.processParameters(JAXRSUtils.java:646)
at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.processRequest(JAXRSInInterceptor.java:237)
at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.handleMessage(JAXRSInInterceptor.java:98)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:271)
at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:239)
at org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:218)
at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:163)
at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:137)
at org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke(CXFNonSpringServlet.java:158)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:243)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.doGet(AbstractHTTPServlet.java:168)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:219)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:908)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:931)
at java.lang.Thread.run(Thread.java:773)
Caused by: java.lang.NullPointerException
at org.apache.cxf.attachment.AttachmentUtil.createAttachment(AttachmentUtil.java:294)
at org.apache.cxf.jaxrs.ext.MessageContextImpl.createAttachments(MessageContextImpl.java:252)
at org.apache.cxf.jaxrs.ext.MessageContextImpl.get(MessageContextImpl.java:75)
at org.apache.cxf.jaxrs.impl.tl.ThreadLocalMessageContext.get(ThreadLocalMessageContext.java:38)
at org.apache.cxf.jaxrs.utils.multipart.AttachmentUtils.getMultipartBody(AttachmentUtils.java:56)
at org.apache.cxf.jaxrs.provider.FormEncodingProvider.readFrom(FormEncodingProvider.java:100)
... 33 more
4

2 回答 2

1

最后,我找到了很长的@QueryParam 注释列表。没有那么糟糕,至少它暴露了一个清晰的契约,并且每个参数都可以具体记录。

于 2013-06-29T16:10:06.520 回答
0

您可以使用 @BeanParam 映射多个查询参数。您可以创建一个包含您想要通过 URL 发送的所有参数的 bean,并使用 @BeanParam 注释将该 bean 添加到 restful 服务中。您的所有参数都会累积到该 bean 中。

于 2014-04-29T12:51:54.560 回答