我希望你能帮助我。
我正在尝试使用JAX-WS创建Java SOAP客户端。
我使用wsimport导入了所有Web服务功能, 为此我必须使用-Xauthfile提供auth文件,auth文件包含以下内容:
http://username:password@server.com:80/Windchill/servlet/MathService?wsdl
我已将所有类导入到eclipse中,并且我尝试使用以下代码调用MathService add函数:
import javax.xml.ws.BindingProvider;
import com.ptc.jws.service.org.myorg.mathservice.MathServiceImpl;
import com.ptc.jws.service.org.myorg.mathservice.MathServiceImplService;
public class ClientStart {
public static void main(String[] args) throws Exception {
MathServiceImplService service = new MathServiceImplService();
MathServiceImpl port = service.getMathServiceImplPort();
// Configure service endpoint (override defined one of the WSDL)
BindingProvider binding = (BindingProvider) port;
binding.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "http://server.com/Windchill/servlet/MathService");
// Add HTTP Basic Authentification credentials to this request
binding.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "username");
binding.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "password");
port.add(1, 1);
}
}
我尝试过/不覆盖服务端点
当我运行代码时,我收到以下错误:
Exception in thread "main" javax.xml.ws.WebServiceException: Failed to access the WSDL at: http://server.com/Windchill/servlet/MathService?wsdl. It failed with:
Got Server returned HTTP response code: 401 for URL: http://server.com/Windchill/servlet/MathService?wsdl while opening stream from http://server.com/Windchill/servlet/MathService?wsdl.
我也遇到了这个错误:
WARNING: WSP0075: Policy assertion "{http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702}EncryptedParts" was evaluated as "UNKNOWN".
Mar 01, 2016 4:29:13 PM [com.sun.xml.internal.ws.policy.EffectiveAlternativeSelector] selectAlternatives
MathServiceImpl.java文件如下:
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;
import javax.xml.ws.WebServiceException;
import javax.xml.ws.WebServiceFeature;
/**
* This class was generated by the JAX-WS RI.
* JAX-WS RI 2.2.4-b01
* Generated source version: 2.2
*
*/
@WebServiceClient(name = "MathServiceImplService", targetNamespace = "http://MathService.myorg.org.service.jws.ptc.com/", wsdlLocation = "http://server.com/Windchill/servlet/MathService?wsdl")
public class MathServiceImplService extends Service
{
private final static URL MATHSERVICEIMPLSERVICE_WSDL_LOCATION;
private final static WebServiceException MATHSERVICEIMPLSERVICE_EXCEPTION;
private final static QName MATHSERVICEIMPLSERVICE_QNAME = new QName("http://MathService.myorg.org.service.jws.ptc.com/", "MathServiceImplService");
static {
URL url = null;
WebServiceException e = null;
try {
url = new URL("http://server.com/Windchill/servlet/MathService?wsdl");
} catch (MalformedURLException ex) {
e = new WebServiceException(ex);
}
MATHSERVICEIMPLSERVICE_WSDL_LOCATION = url;
MATHSERVICEIMPLSERVICE_EXCEPTION = e;
}
public MathServiceImplService() {
super(__getWsdlLocation(), MATHSERVICEIMPLSERVICE_QNAME);
}
public MathServiceImplService(WebServiceFeature... features) {
super(__getWsdlLocation(), MATHSERVICEIMPLSERVICE_QNAME, features);
}
public MathServiceImplService(URL wsdlLocation) {
super(wsdlLocation, MATHSERVICEIMPLSERVICE_QNAME);
}
public MathServiceImplService(URL wsdlLocation, WebServiceFeature... features) {
super(wsdlLocation, MATHSERVICEIMPLSERVICE_QNAME, features);
}
public MathServiceImplService(URL wsdlLocation, QName serviceName) {
super(wsdlLocation, serviceName);
}
public MathServiceImplService(URL wsdlLocation, QName serviceName, WebServiceFeature... features) {
super(wsdlLocation, serviceName, features);
}
/**
*
* @return
* returns MathServiceImpl
*/
@WebEndpoint(name = "MathServiceImplPort")
public MathServiceImpl getMathServiceImplPort() {
return super.getPort(new QName("http://MathService.myorg.org.service.jws.ptc.com/", "MathServiceImplPort"), MathServiceImpl.class);
}
/**
*
* @param features
* A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy. Supported features not in the <code>features</code> parameter will have their default values.
* @return
* returns MathServiceImpl
*/
@WebEndpoint(name = "MathServiceImplPort")
public MathServiceImpl getMathServiceImplPort(WebServiceFeature... features) {
return super.getPort(new QName("http://MathService.myorg.org.service.jws.ptc.com/", "MathServiceImplPort"), MathServiceImpl.class, features);
}
private static URL __getWsdlLocation() {
if (MATHSERVICEIMPLSERVICE_EXCEPTION!= null) {
throw MATHSERVICEIMPLSERVICE_EXCEPTION;
}
return MATHSERVICEIMPLSERVICE_WSDL_LOCATION;
}
}
任何帮助将不胜感激!谢谢!
添
答案 0 :(得分:2)
401 HTTP响应似乎来自为WSDL定义本身提供服务的应用程序。我认为最好(尽可能)下载WSDL并将其捆绑在应用程序资源中。
然后你必须将JAX-WS生成的文件中的URL(可能是MathServiceImplService.java
)从http://server.com/xxx.wsdl
更改为某个URL,如
MathServiceImplService.class.getResource("/path/to/wsdl.wsdl");
答案 1 :(得分:0)
我使用wsimport
来生成代码,并且遇到相同的问题。 Authenticator
为我工作。在启动服务之前添加以下代码。
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("<UserName>", "<Password>".toCharArray());
}
});
MathServiceImplService service = new MathServiceImplService();
...