javax.xml.ws.webserviceexception:无法使用api servlet访问wsdl

rjjhvcjd  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(313)

下面的代码对应于javaapi servlet。我需要构造一个xml并通过浏览器发送它。
如果程序不是webapi而是java应用程序,那么它可以正确连接。
我正在尝试运行以下代码。

package api.get;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.transform.TransformerException;
import javax.xml.ws.BindingProvider;

import javax.xml.namespace.QName;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Service;
import javax.xml.ws.handler.MessageContext;
import java.net.Authenticator;
import java.net.MalformedURLException;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.mimerhellas.cypdisserviceclient.ApplicationException_Exception;
import com.mimerhellas.cypdisserviceclient.ProductService;
import com.mimerhellas.cypdisserviceclient.ProductService_Service;
import com.mimerhellas.cypdisserviceclient.SyncList;
import com.mimerhellas.cypdisserviceclient.SyncListItem;
//import com.mimerhellas.cypdisserviceclient.WebServlet;

import ps.PSServlet;

@WebServlet("/get_products")
public class GetProducts extends PSServlet{

    public GetProducts() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }
    /**
     * @param args the command line arguments
     * @throws IOException 
     * @throws TransformerException 
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {

       try { 

            //construct web service client
            ProductService_Service service = new ProductService_Service();
            ProductService port = service.getProductServicePort();
            //Get provider to set request properties
            BindingProvider provider = (BindingProvider) port;
            //Set request properties
            provider.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
                    "myurl");
            provider.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "user");
            provider.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "pass");

            //Current date is the default web service parameter value
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            Date date = new Date();

            date = formatter.parse("2010-01-01 00:50:00");
            //Prepare wrapper for serialization
            Products productsWrapper = new Products();
            productsWrapper.setProducts(new ArrayList<>());
            //Call to get list of items require sync
            SyncList syncListResponse = port.getSyncList(date);
            for(SyncListItem syncListItem : syncListResponse.getSyncListItem()){
                productsWrapper.getProducts().add(port.getProductByDrugidPacknr(syncListItem.getDrugid(), syncListItem.getPacknr()));
            }

            //Prepare output file
                        String outputFile =  File.separatorChar + new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss").format(new Date()) + "_call.xml";

            //Serialize to output file
            //jaxbMarshaller.marshal(productsWrapper, new File(outputFile));
            //Print message
            System.out.println("data for date " + formatter.format(date) + " were saved to '" + outputFile + "'.");

            response.setContentType("application/xml");
            response.setHeader("Content-Disposition", "attachment; filename=" + new File(outputFile));
            response.setStatus(200);

            //Initialize JAXB
            final JAXBContext jaxbContext = JAXBContext.newInstance(Products.class);
            final Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            jaxbMarshaller.marshal(productsWrapper, response.getOutputStream());

        } catch (JAXBException | ParseException | ApplicationException_Exception ex) {
            Logger.getLogger(GetProducts.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

}

我得到的错误日志如下:

javax.xml.ws.WebServiceException: Failed to access the WSDL at: myurl. It failed with: 
    Server returned HTTP response code: 401 for URL: myurl.
    com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.tryWithMex(Unknown Source)
    com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.parse(Unknown Source)
    com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.parse(Unknown Source)
    com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.parse(Unknown Source)
    com.sun.xml.internal.ws.client.WSServiceDelegate.parseWSDL(Unknown Source)
    com.sun.xml.internal.ws.client.WSServiceDelegate.<init>(Unknown Source)
    com.sun.xml.internal.ws.client.WSServiceDelegate.<init>(Unknown Source)
    com.sun.xml.internal.ws.client.WSServiceDelegate.<init>(Unknown Source)
    com.sun.xml.internal.ws.client.WSServiceDelegate.<init>(Unknown Source)
    com.sun.xml.internal.ws.spi.ProviderImpl.createServiceDelegate(Unknown Source)
    javax.xml.ws.Service.<init>(Unknown Source)
    com.mimerhellas.cypdisserviceclient.ProductService_Service.<init>(ProductService_Service.java:42)
    api.get.GetProducts.doPost(GetProducts.java:80)
    api.get.GetProducts.doGet(GetProducts.java:68)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题