in my post java webservices wsimport - parsing WSDL [ERROR] Server returned HTTP response code: 401 for URL: http://?wsdl needs authorization
i explained how i got an authentication error when trying to create webservices client code using wsimport and how i fixed the problem.
when i tried using the code to test my webservice i got a similar error when running my test code:
Exception in thread "main" javax.xml.ws.WebServiceException: Failed to access the WSDL at: http://localhost:9001/wservice3/services/AdminWebService?wsdl. It failed with:
Server returned HTTP response code: 401 for URL: http://localhost:9001/wservice3/services/AdminWebService?wsdl.
at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.tryWithMex(RuntimeWSDLParser.java:151)
at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.parse(RuntimeWSDLParser.java:133)
at com.sun.xml.internal.ws.client.WSServiceDelegate.parseWSDL(WSServiceDelegate.java:254)
at com.sun.xml.internal.ws.client.WSServiceDelegate.<init>(WSServiceDelegate.java:217)
at com.sun.xml.internal.ws.client.WSServiceDelegate.<init>(WSServiceDelegate.java:165)
at com.sun.xml.internal.ws.spi.ProviderImpl.createServiceDelegate(ProviderImpl.java:93)
at javax.xml.ws.Service.<init>(Service.java:76)
at com.myapp.test.wservice3.server3.AdminWebServiceService.<init>(AdminWebServiceService.java:46)
at com.myapp.test.wservice3.server3.Test.main(Test.java:8)
Caused by: java.io.IOException: Server returned HTTP response code: 401 for URL: http://localhost:9001/wservice3/services/AdminWebService?wsdl
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1403)
at java.net.URL.openStream(URL.java:1031)
at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.createReader(RuntimeWSDLParser.java:793)
at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.resolveWSDL(RuntimeWSDLParser.java:251)
at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.parse(RuntimeWSDLParser.java:118)
... 7 more
it's basically the same error i got when running wsimport because the service requires authentication to fix the problem i had to insert authorization code into the service.
here's a sample of the webservice code generated by wsimport in AdminWebServiceService:
static {
URL url = null;
try {
URL baseUrl;
baseUrl = com.myapp.test.wservice3.server3.AdminWebServiceService.class.getResource(".");
url = new URL(baseUrl, "http://localhost:9001/wservice3/services/AdminWebService?wsdl");
} catch (MalformedURLException e) {
logger.warning("Failed to create URL for the wsdl Location: 'http://localhost:9001/wservice3/services/AdminWebService?wsdl', retrying as a local file");
logger.warning(e.getMessage());
}
ADMINWEBSERVICESERVICE_WSDL_LOCATION = url;
}
to get things rolling i had to make the code look like this:
static {
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
"myusername",
"mypassword".toCharArray());
}
});
URL url = null;
try {
URL baseUrl;
baseUrl = com.myapp.test.wservice3.server3.AdminWebServiceService.class.getResource(".");
url = new URL(baseUrl, "http://localhost:9001/wservice3/services/AdminWebService?wsdl");
} catch (MalformedURLException e) {
logger.warning("Failed to create URL for the wsdl Location: 'http://localhost:9001/wservice3/services/AdminWebService?wsdl', retrying as a local file");
logger.warning(e.getMessage());
}
ADMINWEBSERVICESERVICE_WSDL_LOCATION = url;
}
posts that helped me figure this out:
http://www.xinotes.org/notes/note/1081/
http://etfdevlab.blogspot.no/2009/12/http-basic-authentication-with-jax-ws.html
in the last post it mentions putting the Authenticator code in the constructor, but that wasn't the case for my code generated by wsimport. i had to put the Authenticator code into the static block at the top of the webservice service class.
here's what the full webservice service class code looks like:
package com.myapp.test.wservice3.server3;
import java.net.Authenticator;
import java.net.MalformedURLException;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.util.logging.Logger;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;
import javax.xml.ws.WebServiceFeature;
@WebServiceClient(name = "AdminWebServiceService", targetNamespace = "http://server3.wservice3", wsdlLocation = "http://localhost:9001/wservice3/services/AdminWebService?wsdl")
public class AdminWebServiceService
extends Service
{
private final static URL ADMINWEBSERVICESERVICE_WSDL_LOCATION;
private final static Logger logger = Logger.getLogger(com.myapp.test.wservice3.server3.AdminWebServiceService.class.getName());
static {
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
"myusername",
"mypassword".toCharArray());
}
});
URL url = null;
try {
URL baseUrl;
baseUrl = com.myapp.test.wservice3.server3.AdminWebServiceService.class.getResource(".");
url = new URL(baseUrl, "http://localhost:9001/wservice3/services/AdminWebService?wsdl");
} catch (MalformedURLException e) {
logger.warning("Failed to create URL for the wsdl Location: 'http://localhost:9001/wservice3/services/AdminWebService?wsdl', retrying as a local file");
logger.warning(e.getMessage());
}
ADMINWEBSERVICESERVICE_WSDL_LOCATION = url;
}
public AdminWebServiceService(URL wsdlLocation, QName serviceName) {
super(wsdlLocation, serviceName);
}
public AdminWebServiceService() {
super(ADMINWEBSERVICESERVICE_WSDL_LOCATION, new QName("http://server3.wservice3", "AdminWebServiceService"));
}
@WebEndpoint(name = "AdminWebService")
public AdminWebService getAdminWebService() {
return super.getPort(new QName("http://server3.wservice3", "AdminWebService"), AdminWebService.class);
}
@WebEndpoint(name = "AdminWebService")
public AdminWebService getAdminWebService(WebServiceFeature... features) {
return super.getPort(new QName("http://server3.wservice3", "AdminWebService"), AdminWebService.class, features);
}
}
and here's what my test class looks like that calls the webservice:
package com.myapp.test.wservice3.server3;
public class Test {
public static void main(String[] args) {
AdminWebServiceService awss = new AdminWebServiceService();
AdminWebService aws = awss.getAdminWebService();
System.out.println(aws.maxIdFromMessageCounter());
}
}
IT, computer and programming tutorials and tips that i couldnt find anywhere else using google, from my daily work as a Senior Developer of solutions using Java and Linux.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment