我是java servlet过滤器的新手,我有一个CXF Web服务A.
A的地址是:http://localhost:8080/AdditionWS/services/additioncls?wsdl
我想实现代理Web服务B.
B的地址是: http://localhost:8080/ForwardWS/services/ForwardClsPort?wsdl
为了将请求转发到A到B,我向B:
添加了一个servlet过滤器public class RequestFilter implements Filter {
public void doFilter(final ServletRequest request, final ServletResponse response, FilterChain chain)
throws java.io.IOException, javax.servlet.ServletException {
HttpServletResponse resp = (HttpServletResponse) response;
String redirect="http://localhost:8080/AdditionWS/services/additioncls";
Enumeration<String> enumeration=request.getParameterNames();
String parameter=null;
while(enumeration.hasMoreElements())
{
parameter=enumeration.nextElement();
if(parameter.toLowerCase()=="wsdl")
break;
}
// if the parameter 'wsdl' exists forward to http://localhost:8080/AdditionWS/services/additioncls?wsdl
if(parameter!=null)
redirect="http://localhost:8080/AdditionWS/services/additioncls?"+"wsdl";
redirect=resp.encodeRedirectURL(redirect);
resp.sendRedirect(redirect);
}
}
我在客户端代码中更新了网址,如下所示:
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;
import javax.xml.ws.WebServiceFeature;
import javax.xml.ws.Service;
//before:
wsdlLocation=http://localhost:8080/AdditionWS/services/additioncls?wsdl
@WebServiceClient(name = "AdditionClsService",
wsdlLocation = "http://localhost:8080/ForwardWS/services/ForwardClsPort?wsdl",
targetNamespace = "http://logic/")
public class AdditionClsService extends Service {
public final static URL WSDL_LOCATION;
public final static QName SERVICE = new QName("http://logic/", "AdditionClsService");
public final static QName AdditionClsPort = new QName("http://logic/", "AdditionClsPort");
static {
URL url = null;
try {
// before url=new URL("http://localhost:8080/AdditionWS/services/additioncls?wsdl");
url = new URL("http://localhost:8080/ForwardWS/services/ForwardClsPort?wsdl");
} catch (MalformedURLException e) {
java.util.logging.Logger.getLogger(AdditionClsService.class.getName())
.log(java.util.logging.Level.INFO,
"Can not initialize the default wsdl from {0}", "http://localhost:8080/AdditionWS/services/additioncls?wsdl");
}
WSDL_LOCATION = url;
}
public AdditionClsService(URL wsdlLocation) {
super(wsdlLocation, SERVICE);
}
public AdditionClsService(URL wsdlLocation, QName serviceName) {
super(wsdlLocation, serviceName);
}
public AdditionClsService() {
super(WSDL_LOCATION, SERVICE);
}
public AdditionClsService(WebServiceFeature ... features) {
super(WSDL_LOCATION, SERVICE, features);
}
public AdditionClsService(URL wsdlLocation, WebServiceFeature ... features) {
super(wsdlLocation, SERVICE, features);
}
public AdditionClsService(URL wsdlLocation, QName serviceName, WebServiceFeature ... features) {
super(wsdlLocation, serviceName, features);
}
/**
*
* @return
* returns AdditionWSSEI
*/
@WebEndpoint(name = "AdditionClsPort")
public AdditionWSSEI getAdditionClsPort() {
return super.getPort(AdditionClsPort, AdditionWSSEI.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 AdditionWSSEI
*/
@WebEndpoint(name = "AdditionClsPort")
public AdditionWSSEI getAdditionClsPort(WebServiceFeature... features) {
return super.getPort(AdditionClsPort, AdditionWSSEI.class, features);
}
}
由于某种原因,客户端请求不会转发到A,我收到以下错误:
Exception in thread "main" javax.xml.ws.WebServiceException: org.apache.cxf.service.factory.ServiceConstructionException: Failed to create service.
at org.apache.cxf.jaxws.ServiceImpl.initialize(ServiceImpl.java:162)
at org.apache.cxf.jaxws.ServiceImpl.<init>(ServiceImpl.java:129)
at org.apache.cxf.jaxws.spi.ProviderImpl.createServiceDelegate(ProviderImpl.java:82)
at javax.xml.ws.Service.<init>(Service.java:77)
at business.AdditionClsService.<init>(AdditionClsService.java:43)
at business.AdditionWSSEI_AdditionClsPort_Client.main(AdditionWSSEI_AdditionClsPort_Client.java:49)
Caused by: org.apache.cxf.service.factory.ServiceConstructionException: Failed to create service.
at org.apache.cxf.wsdl11.WSDLServiceFactory.<init>(WSDLServiceFactory.java:87)
at org.apache.cxf.jaxws.ServiceImpl.initializePorts(ServiceImpl.java:217)
at org.apache.cxf.jaxws.ServiceImpl.initialize(ServiceImpl.java:160)
... 5 more
Caused by: javax.wsdl.WSDLException: WSDLException: faultCode=PARSER_ERROR: com.ctc.wstx.exc.WstxEOFException: Unexpected EOF in prolog
at [row,col,system-id]: [1,0,"http://localhost:8080/ForwardWS/services/ForwardClsPort?wsdl"]
at org.apache.cxf.wsdl11.WSDLManagerImpl.loadDefinition(WSDLManagerImpl.java:228)
at org.apache.cxf.wsdl11.WSDLManagerImpl.getDefinition(WSDLManagerImpl.java:163)
at org.apache.cxf.wsdl11.WSDLServiceFactory.<init>(WSDLServiceFactory.java:85)
... 7 more
Caused by: com.ctc.wstx.exc.WstxEOFException: Unexpected EOF in prolog
at [row,col,system-id]: [1,0,"http://localhost:8080/ForwardWS/services/ForwardClsPort?wsdl"]
at com.ctc.wstx.sr.StreamScanner.throwUnexpectedEOF(StreamScanner.java:685)
at com.ctc.wstx.sr.BasicStreamReader.handleEOF(BasicStreamReader.java:2141)
at com.ctc.wstx.sr.BasicStreamReader.nextFromProlog(BasicStreamReader.java:2047)
at com.ctc.wstx.sr.BasicStreamReader.next(BasicStreamReader.java:1131)
at org.apache.cxf.staxutils.StaxUtils.readDocElements(StaxUtils.java:1369)
at org.apache.cxf.staxutils.StaxUtils.readDocElements(StaxUtils.java:1263)
at org.apache.cxf.staxutils.StaxUtils.read(StaxUtils.java:1191)
at org.apache.cxf.wsdl11.WSDLManagerImpl.loadDefinition(WSDLManagerImpl.java:219)
... 9 more
我是否遗漏了某些内容,或者是否有不同的方法来实现将客户端请求转发给A或其中一个副本的代理cxf Web服务? 谢谢!