解析SOAP XML响应,错误:org.xml.sax.SAXParseException; lineNumber:1; columnNumber:1; prolog

时间:2018-03-02 06:41:01

标签: java jsp servlets soap wsdl

预期输出:
- 通过输入ZipCode作为输入获取供应商详细信息

问题:
  - 在 SupplierDetail.java 中的try catch块的第3行显示错误   - 虽然解组它会抛出异常。请参阅错误日志

  

javax.xml.bind.UnmarshalException - 包含链接异常:   [org.xml.sax.SAXParseException; lineNumber:1; columnNumber:1;   prolog中不允许使用内容。]

WSDL
端点 http://www.webservicex.net/medicareSupplier.asmx?WSDL

将GetSupplierByZipCode方法用于给定WSDL的供应商详细信息

步骤1:使用wsimport工具生成存根

  

wsimport -keep -d" path"   http://www.webservicex.net/medicareSupplier.asmx?WSDL

第2步:创建动态Web项目 1.将生成的文件添加到项目

以下是代码
的index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>MediCare SOAP Client </title>
</head>
<body>
<h2>Hello JSP</h2>

<form method="post" action="supplierdetail">
<label>Zip Code</label>
<input type="text" name="searchBarInputId">
<input type="submit" value="Get Supplier">
</form>

</body>
</html>

SupplierDetail.java

package com.supplier;

import java.io.IOException;
import java.io.StringReader;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;


import net.webservicex.MediCareSupplier;
import net.webservicex.MediCareSupplierSoap;
import net.webservicex.SupplierData;

public class SupplierDetail extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("doGet");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String cityZipCode = request.getParameter("searchBarInputId");
        System.out.println("doPost:"+ cityZipCode);
        MediCareSupplier mediCareObj  = new MediCareSupplier();
        MediCareSupplierSoap mediCareSupplier = mediCareObj.getMediCareSupplierSoap();
        mediCareSupplier.getSupplierByZipCode(cityZipCode, null, null);

        try {
            JAXBContext jAXBContext = JAXBContext.newInstance(SupplierData.class);
            Unmarshaller unmarshaller = jAXBContext.createUnmarshaller();
            SupplierData data = (SupplierData)unmarshaller.unmarshal(new StringReader(response.toString()));
            System.out.println("SupplierDAta: "+data+':'+ data.getSupplierNumber()+ "\n"+data.getAddress1());

            /* RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/SupplierDetails.jsp");
             dispatcher.forward(request, response);*/ 

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

错误日志:

  

信息:服务器启动时间为610毫秒doPost:61244   javax.xml.bind.UnmarshalException     - 链接异常:[org.xml.sax.SAXParseException; lineNumber:1; columnNumber:1; prolog中不允许内容。] at   javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(未知   来源)at   com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(未知   来源)at   com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(未知   来源)at   com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(未知   来源)at   javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(未知   来源)at   javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(未知   来源)com.supplier.SupplierDetail.doPost(SupplierDetail.java:36)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:661)at   javax.servlet.http.HttpServlet.service(HttpServlet.java:742)at at   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)     在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)     在   org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)     在   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)     在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)     在   org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)     在   org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)     在   org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)     在   org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)     在   org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)     在   org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)     在   org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)     在   org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)     在   org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)     在   org.apache.coyote.AbstractProtocol $ ConnectionHandler.process(AbstractProtocol.java:790)     在   org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1459)     在   org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)     at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)     at java.util.concurrent.ThreadPoolExecutor $ Worker.run(Unknown Source)     在   org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)     在java.lang.Thread.run(未知来源)引起:   org.xml.sax.SAXParseException; lineNumber:1; columnNumber:1;内容   不允许在prolog中。在   com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(未知   来源)at   com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(未知   来源)at   com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(未知   来源)at   com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(未知   来源)at   com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(未知   来源)at   com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl $ PrologDriver.next(未知   来源)at   com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(未知   来源)at   com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(未知   来源)at   com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(未知   来源)at   com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(未知   来源)at   com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(未知   来源)at   com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(未知   来源)at   com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(未知   来源)at   com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl $ JAXPSAXParser.parse(未知   来源)... 28更多

请帮助..先谢谢

2 个答案:

答案 0 :(得分:0)

你为什么要尝试解组?以下代码有什么意义?.vscode\launch

如果您的目的是通过此SupplierData data = (SupplierData)unmarshaller.unmarshal(new StringReader(response.toString()));获得SOAP,则已经拥有它。(我假设您的代码在try块之前正确执行)。

答案 1 :(得分:0)

找到了另一个解决方案

返回多个值

使用SOAP可以在单个请求中返回多个值。在Java中这是不可能的,因为方法只能返回一个对象。

JAX-WS通过Holder的概念解决了这个问题。 javax.xml.ws.Holder是一个简单的包装对象,可以将其作为参数传递给@WebService方法。应用程序在请求期间设置所有者的值,服务器将把该值作为OUT参数发送回去。

    MediCareSupplier mediCare = new MediCareSupplier();
    MediCareSupplierSoap mediCareSOAP = mediCare.getMediCareSupplierSoap();

    Holder<Boolean> getSupplierByZipCodeResult = new Holder<Boolean>();
    Holder<SupplierDataList> supplierDataLists = new Holder<SupplierDataList>();
    mediCareSOAP.getSupplierByZipCode(zip, getSupplierByZipCodeResult, supplierDataLists);

    List<SupplierData> supplierDataList = supplierDataLists.value.getSupplierDatas().getSupplierData();

循环浏览SupplierDataList并获取所有数据