2016-04-25 49 views
2

我正在學習如何在JAX-WS SOAP Web服務中創建邏輯處理程序。在這裏,我試圖獲取有效載荷數據並想要打印測試。但我遇到問題。如何從SOAP邏輯處理程序獲取有效內容對象

這裏是我LogincalHandler代碼

public class HelloMessengerLogicalHandler implements LogicalHandler<LogicalMessageContext> { 

    public void close(MessageContext ctx) { 
    } 

    public boolean handleFault(LogicalMessageContext ctx) { 
     return false; 
    } 

    public boolean handleMessage(LogicalMessageContext ctx) { 
     Boolean outbound = (Boolean) ctx.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY); 
     if(outbound) { 
      LogicalMessage message = ctx.getMessage(); 
      //Source payload = message.getPayload(); 
      JAXBContext jaxbContext; 
      try { 
       jaxbContext = JAXBContext.newInstance(Person.class); 
       Object jaxbPayload = message.getPayload(jaxbContext); 
       System.out.println(jaxbPayload); 
      } catch (JAXBException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

     } 

     return true; 
    } 
} 

這裏是我的web服務代碼:

@WebService 
@HandlerChain(file = "handler-chain.xml") 
public class HelloMessenger { 
    public Person getPerson(String name) { 
     return new Person(name); 
    } 
} 

這是我Person類:

public class Person { 

    // Default Constructor & Getters, Setters 
    private String name; 

    public Person(String name) { 
     this.name = name; 
    } 
    @Override 
    public String toString() { 
     return "Person [name=" + name + "]"; 
    } 
} 

當我發表我的代碼和訪問web服務我收到以下異常:

javax.xml.ws.WebServiceException: javax.xml.bind.MarshalException 
- with linked exception: 
[com.sun.istack.internal.SAXParseException2; unexpected element (uri:"http://simple/", local:"getPersonResponse"). Expected elements are (none)] 
    at com.sun.xml.internal.ws.handler.LogicalMessageImpl.getPayload(LogicalMessageImpl.java:121) 
    at simple.HelloMessengerLogicalHandler.handleMessage(HelloMessengerLogicalHandler.java:31) 
    at simple.HelloMessengerLogicalHandler.handleMessage(HelloMessengerLogicalHandler.java:1) 
    at com.sun.xml.internal.ws.handler.HandlerProcessor.callHandleMessageReverse(HandlerProcessor.java:326) 
    at com.sun.xml.internal.ws.handler.HandlerProcessor.callHandlersResponse(HandlerProcessor.java:197) 
    at com.sun.xml.internal.ws.handler.ServerLogicalHandlerTube.callHandlersOnResponse(ServerLogicalHandlerTube.java:158) 
    at com.sun.xml.internal.ws.handler.HandlerTube.processResponse(HandlerTube.java:149) 
    at com.sun.xml.internal.ws.api.pipe.Fiber.__doRun(Fiber.java:636) 
    at com.sun.xml.internal.ws.api.pipe.Fiber._doRun(Fiber.java:585) 
    at com.sun.xml.internal.ws.api.pipe.Fiber.doRun(Fiber.java:570) 
    at com.sun.xml.internal.ws.api.pipe.Fiber.runSync(Fiber.java:467) 
    at com.sun.xml.internal.ws.server.WSEndpointImpl$2.process(WSEndpointImpl.java:299) 
    at com.sun.xml.internal.ws.transport.http.HttpAdapter$HttpToolkit.handle(HttpAdapter.java:593) 
    at com.sun.xml.internal.ws.transport.http.HttpAdapter.handle(HttpAdapter.java:244) 
    at com.sun.xml.internal.ws.transport.http.server.WSHttpHandler.handleExchange(WSHttpHandler.java:95) 
    at com.sun.xml.internal.ws.transport.http.server.WSHttpHandler.handle(WSHttpHandler.java:80) 
    at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:77) 
    at sun.net.httpserver.AuthFilter.doFilter(AuthFilter.java:83) 
    at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:80) 
    at sun.net.httpserver.ServerImpl$Exchange$LinkHandler.handle(ServerImpl.java:677) 
    at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:77) 
    at sun.net.httpserver.ServerImpl$Exchange.run(ServerImpl.java:649) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 
    at java.lang.Thread.run(Thread.java:745) 

我是新手,我的理解是有效載荷對象是Person,所以它應該在日誌中顯示person對象,但是我看到這個錯誤。

請解釋這個錯誤是什麼意思&如何獲得邏輯處理程序中的有效載荷數據?

回答

1

Source payload = message.getPayload();

然後通過使用xmltransformation或其他方式將DomSource對象轉換爲字符串,它會給你有效載荷。