javax.xml.ws.WebServiceException:无法使用api servlet访问WSDL

javax.xml.ws.WebServiceException:无法使用api servlet访问WSDL,java,wsdl,wsdl2java,Java,Wsdl,Wsdl2java,我有以下与JavaAPI servlet对应的代码。我需要构造一个xml并通过浏览器发送它 如果程序不是web api而是java应用程序,那么它可以正确连接 我正在尝试运行以下代码 package api.get; import java.io.File; import java.io.IOException; import java.net.MalformedURLException; import java.net.PasswordAuthentication; import java.

我有以下与JavaAPI servlet对应的代码。我需要构造一个xml并通过浏览器发送它

如果程序不是web api而是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);
        }

    }

}
packapi.get;
导入java.io.File;
导入java.io.IOException;
导入java.net.MalformedURLException;
导入java.net.PasswordAuthentication;
导入java.net.URL;
导入java.text.ParseException;
导入java.text.simpleDataFormat;
导入java.util.ArrayList;
导入java.util.Date;
导入java.util.logging.Level;
导入java.util.logging.Logger;
导入javax.servlet.ServletException;
导入javax.servlet.annotation.WebServlet;
导入javax.servlet.http.HttpServletRequest;
导入javax.servlet.http.HttpServletResponse;
导入javax.xml.bind.JAXBContext;
导入javax.xml.bind.JAXBException;
导入javax.xml.bind.Marshaller;
导入javax.xml.transform.TransformerException;
导入javax.xml.ws.BindingProvider;
导入javax.xml.namespace.QName;
导入javax.xml.ws.BindingProvider;
导入javax.xml.ws.Service;
导入javax.xml.ws.handler.MessageContext;
导入java.net.Authenticator;
导入java.net.MalformedURLException;
导入java.net.PasswordAuthentication;
导入java.net.URL;
导入java.util.Collections;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map;
导入com.mimerhellas.cypDiserviceClient.ApplicationException\u异常;
导入com.mimerhellas.cypdiserviceclient.ProductService;
导入com.mimerhellas.cypdiserviceclient.ProductService\u服务;
导入com.mimerhellas.cypdiserviceclient.SyncList;
导入com.mimerhellas.cypDiserviceClient.SyncListItem;
//导入com.mimerhellas.cypdiserviceclient.WebServlet;
导入ps.PSServlet;
@WebServlet(“/get\u产品”)
公共类GetProducts扩展了PSServlet{
公共产品(){
超级();
}
受保护的void doGet(HttpServletRequest请求、HttpServletResponse响应)
抛出ServletException、IOException{
doPost(请求、响应);
}
/**
*@param指定命令行参数
*@抛出异常
*@TransformerException
*/
受保护的void doPost(HttpServletRequest请求、HttpServletResponse响应)
抛出ServletException、IOException{
试试{
//构建web服务客户端
ProductService_服务=新ProductService_服务();
ProductService端口=service.getProductServicePort();
//获取提供程序以设置请求属性
BindingProvider=(BindingProvider)端口;
//设置请求属性
provider.getRequestContext().put(BindingProvider.ENDPOINT\u ADDRESS\u属性,
“myurl”);
provider.getRequestContext().put(BindingProvider.USERNAME_属性,“用户”);
provider.getRequestContext().put(BindingProvider.PASSWORD_属性,“pass”);
//当前日期是默认的web服务参数值
SimpleDataFormat格式化程序=新的SimpleDataFormat(“yyyy-MM-dd”);
日期=新日期();
日期=格式化程序.parse(“2010-01-01 00:50:00”);
//准备用于序列化的包装器
Products ProductsRapper=新产品();
setProducts(新的ArrayList());
//调用以获取需要同步的项目列表
SyncList syncListResponse=port.getSyncList(日期);
对于(SyncListItem SyncListItem:syncListResponse.getSyncListItem()){
productsWrapper.getProducts().add(port.getProductByDrugidPacknr(syncListItem.getDrugid(),syncListItem.getPacknr());
}
//准备输出文件
String outputFile=File.separatorChar+new SimpleDateFormat(“yyyyy\u MM\u dd\u HH\u MM\u ss”).format(new Date())+“\u call.xml”;
//序列化到输出文件
//marshall(productsWrapper,新文件(outputFile));
//打印消息
System.out.println(“日期数据”+formatter.format(date)+”保存到“+outputFile+”);
setContentType(“应用程序/xml”);
response.setHeader(“内容处置”、“附件;文件名=“+新文件(outputFile));
答复:setStatus(200);
//初始化JAXB
final-JAXBContext-JAXBContext=JAXBContext.newInstance(Products.class);
final Marshaller jaxbMarshaller=jaxbContext.createMarshaller();
setProperty(Marshaller.JAXB_格式化的_输出,true);
jaxbMarshaller.marshall(productsWrapper,response.getOutputStream());
}catch(jaxbeexception | ParseException | ApplicationException | u Exception ex){
getLogger.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)
javax.xml.ws.WebServiceException:无法访问位于:myurl的WSDL。失败的原因是:
服务器返回了URL:myurl的HTTP响应代码:401。
com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.tryWithMex(未知源)
com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.parse(未知源)
com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.parse(未知源)
com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.parse(未知源)
com.sun.xml.internal.ws.client.WSServiceDelegate.parseWSDL(未知源)
com.sun.xml.internal.ws.client.WSServiceDelegate。(未知源)
com.sun.xml.internal.ws.client.WSServiceDelegate。(未知源)
com.sun.xml.internal.ws.client.WSServiceDelegate。(未知源)
com.sun.xml.internal.ws.client.WSServiceDelegate。(未知源)