如何在JAX-WS中添加响应头?

时间:2016-09-08 00:23:04

标签: web-services soap jax-ws

我有一个类似的方法:

@WebMethod
    public BeanResponse generar(@WebParam(header=true, mode=Mode.INOUT, name="auditoria", partName="auditoria") Holder<Param> auditoria, 
                                            @WebParam(name="request") BeanRequest request) throws Exception {
//code
}

此网络服务显示此结果:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">  
   <soap:Body>
      <ns2:generarResponse xmlns:ns2="http://url/">
         <return>
            <codRpta>0</codRpta>
            <msgRpta>RESULT</msgRpta>
            <version>v01</version>
            <!--data of bean-->
         </return>
      </ns2:generarResponse>
   </soap:Body>
</soap:Envelope>

我想要这个结果

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Header>
        <res1>0</res1>
        <res2>rpta</res2>
        <res3>v01</res3>
   </soap:Header>   
   <soap:Body>
      <ns2:generarResponse xmlns:ns2="http://url/">
         <return>
            <codRpta>0</codRpta>
            <msgRpta>RESULT</msgRpta>
            <version>v01</version>
            <!--data of bean-->
         </return>
      </ns2:generarResponse>
   </soap:Body>
</soap:Envelope>

我在互联网上搜索,我找到了像this这样的教程,但我想找到一个解决方案,我只应修改bean响应或为web服务方法添加一些anotation。

感谢您的帮助!

修改

我从上面的教程中指导我:

我在handleMessage方法中有这个:

Boolean outboundProperty = (Boolean)
                   context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

        if (outboundProperty.booleanValue()) {

            try {

                Map<String, List<String>> headers = (Map<String, List<String>>)context.get(MessageContext.HTTP_RESPONSE_HEADERS);

                if(context.getMessage().getSOAPPart().getEnvelope() != null)
                    context.getMessage().getSOAPPart().getEnvelope().getHeader().detachNode();

                Map<String, String> headersResponse = (Map<String, String>) context.get(Constants.CUSTOM_HEADERS_SOAP_RESPONSE);

                SOAPHeader soapHeader = context.getMessage().getSOAPPart().getEnvelope().addHeader();
                soapHeader.addHeaderElement(new QName("http://impl.service.paperless.equifax.com.pe/", headersResponse.get(properties.getWrapperResponseHeaderCodRpta()), "ns2"));
                soapHeader.addHeaderElement(new QName("http://impl.service.paperless.equifax.com.pe/", headersResponse.get(properties.getWrapperResponseHeaderDescRpta()), "ns2"));
                soapHeader.addHeaderElement(new QName("http://impl.service.paperless.equifax.com.pe/", headersResponse.get(properties.getWrapperResponseHeaderCodConsulta()), "ns2"));

            } catch (SOAPException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

        return true;

但我在第16行遇到了这个错误:

19:31:01,554 Advertencia [org.apache.cxf.jaxws.handler.HandlerChainInvoker] (http-localhost-127.0.0.1-8080-1) HANDLER_RAISED_RUNTIME_EXCEPTION: java.lang.NullPointerException
    at pe.com.equifax.paperless.handlers.ResponseHandler.handleMessage(ResponseHandler.java:82) [classes:]
    at pe.com.equifax.paperless.handlers.ResponseHandler.handleMessage(ResponseHandler.java:1) [classes:]
    at org.apache.cxf.jaxws.handler.HandlerChainInvoker.invokeHandleMessage(HandlerChainInvoker.java:335)
    at org.apache.cxf.jaxws.handler.HandlerChainInvoker.invokeHandlerChain(HandlerChainInvoker.java:253)
    at org.apache.cxf.jaxws.handler.HandlerChainInvoker.invokeProtocolHandlers(HandlerChainInvoker.java:131)
    at org.apache.cxf.jaxws.handler.soap.SOAPHandlerInterceptor.handleMessageInternal(SOAPHandlerInterceptor.java:168)
    at org.apache.cxf.jaxws.handler.soap.SOAPHandlerInterceptor.access$000(SOAPHandlerInterceptor.java:70)
    at org.apache.cxf.jaxws.handler.soap.SOAPHandlerInterceptor$1.handleMessage(SOAPHandlerInterceptor.java:80)
    at org.apache.cxf.jaxws.handler.soap.SOAPHandlerInterceptor$1.handleMessage(SOAPHandlerInterceptor.java:79)
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:263)
    at org.apache.cxf.interceptor.OutgoingChainInterceptor.handleMessage(OutgoingChainInterceptor.java:77)
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:263)
    at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
    at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:207)
    at org.jboss.wsf.stack.cxf.RequestHandlerImpl.handleHttpRequest(RequestHandlerImpl.java:91)
    at org.jboss.wsf.stack.cxf.transport.ServletHelper.callRequestHandler(ServletHelper.java:169)
    at org.jboss.wsf.stack.cxf.CXFServletExt.invoke(CXFServletExt.java:87)
    at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:185)
    at org.apache.cxf.transport.servlet.AbstractHTTPServlet.doPost(AbstractHTTPServlet.java:108)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:754) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
    at org.jboss.wsf.stack.cxf.CXFServletExt.service(CXFServletExt.java:135)
    at org.jboss.wsf.spi.deployment.WSFServlet.service(WSFServlet.java:140) [jbossws-spi-2.0.3.GA.jar:2.0.3.GA]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.13.Final.jar:]
    at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.13.Final.jar:]
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.13.Final.jar:]
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:]
    at java.lang.Thread.run(Thread.java:722) [rt.jar:1.7.0_05]

19:31:01,622 Advertencia [org.apache.cxf.phase.PhaseInterceptorChain] (http-localhost-127.0.0.1-8080-1) Interceptor for ****** has thrown exception, unwinding now: java.lang.NullPointerException
    at pe.com.equifax.paperless.handlers.ResponseHandler.handleMessage(ResponseHandler.java:82) [classes:]
    at pe.com.equifax.paperless.handlers.ResponseHandler.handleMessage(ResponseHandler.java:1) [classes:]
    at org.apache.cxf.jaxws.handler.HandlerChainInvoker.invokeHandleMessage(HandlerChainInvoker.java:335)
    at org.apache.cxf.jaxws.handler.HandlerChainInvoker.invokeHandlerChain(HandlerChainInvoker.java:253)
    at org.apache.cxf.jaxws.handler.HandlerChainInvoker.invokeProtocolHandlers(HandlerChainInvoker.java:131)
    at org.apache.cxf.jaxws.handler.soap.SOAPHandlerInterceptor.handleMessageInternal(SOAPHandlerInterceptor.java:168)
    at org.apache.cxf.jaxws.handler.soap.SOAPHandlerInterceptor.access$000(SOAPHandlerInterceptor.java:70)
    at org.apache.cxf.jaxws.handler.soap.SOAPHandlerInterceptor$1.handleMessage(SOAPHandlerInterceptor.java:80)
    at org.apache.cxf.jaxws.handler.soap.SOAPHandlerInterceptor$1.handleMessage(SOAPHandlerInterceptor.java:79)
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:263)
    at org.apache.cxf.interceptor.OutgoingChainInterceptor.handleMessage(OutgoingChainInterceptor.java:77)
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:263)
    at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
    at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:207)
    at org.jboss.wsf.stack.cxf.RequestHandlerImpl.handleHttpRequest(RequestHandlerImpl.java:91)
    at org.jboss.wsf.stack.cxf.transport.ServletHelper.callRequestHandler(ServletHelper.java:169)
    at org.jboss.wsf.stack.cxf.CXFServletExt.invoke(CXFServletExt.java:87)
    at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:185)
    at org.apache.cxf.transport.servlet.AbstractHTTPServlet.doPost(AbstractHTTPServlet.java:108)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:754) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
    at org.jboss.wsf.stack.cxf.CXFServletExt.service(CXFServletExt.java:135)
    at org.jboss.wsf.spi.deployment.WSFServlet.service(WSFServlet.java:140) [jbossws-spi-2.0.3.GA.jar:2.0.3.GA]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.13.Final.jar:]
    at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.13.Final.jar:]
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.13.Final.jar:]
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:]
    at java.lang.Thread.run(Thread.java:722) [rt.jar:1.7.0_05]

编辑2

我不知道它是否与问题有关系,当我部署Web应用程序时,我在控制台中收到此错误(无论如何Web应用程序已成功部署):

11:13:57,084 WARN  [org.jboss.as.ee] (MSC service thread 1-8) JBAS011006: Not installing optional component org.springframework.http.server.ServletServerHttpAsyncRequestControl due to exception: org.jboss.as.server.deployment.DeploymentUnitProcessingException: JBAS011054: Could not find default constructor for class org.springframework.http.server.ServletServerHttpAsyncRequestControl
    at org.jboss.as.ee.component.ComponentDescription$DefaultComponentConfigurator.configure(ComponentDescription.java:606)
    at org.jboss.as.ee.component.deployers.EEModuleConfigurationProcessor.deploy(EEModuleConfigurationProcessor.java:81)
    at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:113) [jboss-as-server-7.1.1.Final.jar:7.1.1.Final]
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) [rt.jar:1.7.0_05]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) [rt.jar:1.7.0_05]
    at java.lang.Thread.run(Thread.java:722) [rt.jar:1.7.0_05]

11:13:57,092 WARN  [org.jboss.as.ee] (MSC service thread 1-8) JBAS011006: Not installing optional component org.springframework.web.context.request.async.StandardServletAsyncWebRequest due to exception: org.jboss.as.server.deployment.DeploymentUnitProcessingException: JBAS011054: Could not find default constructor for class org.springframework.web.context.request.async.StandardServletAsyncWebRequest
    at org.jboss.as.ee.component.ComponentDescription$DefaultComponentConfigurator.configure(ComponentDescription.java:606)
    at org.jboss.as.ee.component.deployers.EEModuleConfigurationProcessor.deploy(EEModuleConfigurationProcessor.java:81)
    at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:113) [jboss-as-server-7.1.1.Final.jar:7.1.1.Final]
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) [rt.jar:1.7.0_05]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) [rt.jar:1.7.0_05]
    at java.lang.Thread.run(Thread.java:722) [rt.jar:1.7.0_05]

1 个答案:

答案 0 :(得分:0)

您可以将标题定义为方法@WebParam(mode = OUT,header = true)参数:

item_id

另外,我建议将res1 res2和res3字符串包装到complexType包装器中,这样你只有一个带有mode = OUT

的标头