为什么网络服务和代理客户端连接不上?
Why web service and proxy client not connecting?
我有一个应用程序,我尝试在其中组合 Spring MVC
和 Apache CFX
(soap) Web 服务。当我 运行 只是应用程序时,一切似乎都很好,我看到通过这个 link(http://localhost:8080/services/customer?wsdl
) 生成的 WSDL。但是当我 运行 测试时,它抛出 WebServiceException: Could not send Message
... Connection refused
.
我已经通过 Windows Firewall Defender
打开了 public、私有和域区域的所有端口。也许我错过了什么。
在绝望的调查中,我用这个命令 (wsimport -keep -verbose http://localhost:8080/services/customer?wsdl
) 检查了 link。结果,它给出了这个:
[ERROR] Server returned HTTP response code: 403 for URL: http://localhost:8080/services/customer?wsdl
Failed to read the WSDL document: http://localhost:8080/services/customer?wsdl, because 1) could not find the document; /2) the document could not be read; 3) the root element of the document is not <wsdl:definitions>.
[ERROR] Could not find wsdl:service in the provided WSDL(s):
At least one WSDL with at least one service definition needs to be provided.
Now I do not know which way to dig.
WebServiceDispatcherServletInitializer
public class WebServiceDispatcherServletInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(WebServiceConfig.class);
servletContext.addListener(new ContextLoaderListener(context));
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new CXFServlet());
dispatcher.addMapping("/services/*");
}
}
网络服务配置
@Configuration
public class WebServiceConfig {
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(springBus(), new CustomerWebServiceImpl() );
endpoint.publish("http://localhost:8080/services/customer");
return endpoint;
}
}
客户端配置
@Configuration
public class ClientConfig {
@Bean(name = "client")
public Object generateProxy() {
return proxyFactoryBean().create();
}
@Bean
public JaxWsProxyFactoryBean proxyFactoryBean() {
JaxWsProxyFactoryBean proxyFactory = new JaxWsProxyFactoryBean();
proxyFactory.setServiceClass(CustomerWebService.class);
proxyFactory.setAddress("http://localhost:8080/services/customer");
return proxyFactory;
}
}
CustomerWebServiceImplTest
@ActiveProfiles(profiles = "test")
@ContextConfiguration(classes = {
PersistenceConfig.class,
RootConfig.class,
WebServiceConfig.class,
ClientConfig.class
})
@WebAppConfiguration
public class CustomerWebServiceImplTest {
private ApplicationContext context = new AnnotationConfigApplicationContext(ClientConfig.class);
private CustomerWebService customerWsProxy = (CustomerWebService) context.getBean("client");
@Test
public void addCustomer() {
CustomerDto customer = new CustomerDto();
customer.setName("John");
assertEquals("Hello " + customer.getName(), customerWsProxy.addCustomer(customer));
}
}
您能否提示错误可能出在哪里?
UPD:我在我和我的应用程序拥有完全访问权限的 PC 上检查了这个设置,它仍然抛出异常。
解决方案非常简单 - 只需添加 @RunWith(SpringRunner.class)
。因为这个注解是运行springbean,而不是@WebAppConfiguration
和@ContextConfiguration
。
这就是它的样子
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {
RootConfig.class,
WebServiceConfig.class,
ClientConfig.class
})
public class CustomerWebServiceImplTest {
...
}
我有一个应用程序,我尝试在其中组合 Spring MVC
和 Apache CFX
(soap) Web 服务。当我 运行 只是应用程序时,一切似乎都很好,我看到通过这个 link(http://localhost:8080/services/customer?wsdl
) 生成的 WSDL。但是当我 运行 测试时,它抛出 WebServiceException: Could not send Message
... Connection refused
.
我已经通过 Windows Firewall Defender
打开了 public、私有和域区域的所有端口。也许我错过了什么。
在绝望的调查中,我用这个命令 (wsimport -keep -verbose http://localhost:8080/services/customer?wsdl
) 检查了 link。结果,它给出了这个:
[ERROR] Server returned HTTP response code: 403 for URL: http://localhost:8080/services/customer?wsdl
Failed to read the WSDL document: http://localhost:8080/services/customer?wsdl, because 1) could not find the document; /2) the document could not be read; 3) the root element of the document is not <wsdl:definitions>.
[ERROR] Could not find wsdl:service in the provided WSDL(s):
At least one WSDL with at least one service definition needs to be provided.
Now I do not know which way to dig.
WebServiceDispatcherServletInitializer
public class WebServiceDispatcherServletInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(WebServiceConfig.class);
servletContext.addListener(new ContextLoaderListener(context));
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new CXFServlet());
dispatcher.addMapping("/services/*");
}
}
网络服务配置
@Configuration
public class WebServiceConfig {
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(springBus(), new CustomerWebServiceImpl() );
endpoint.publish("http://localhost:8080/services/customer");
return endpoint;
}
}
客户端配置
@Configuration
public class ClientConfig {
@Bean(name = "client")
public Object generateProxy() {
return proxyFactoryBean().create();
}
@Bean
public JaxWsProxyFactoryBean proxyFactoryBean() {
JaxWsProxyFactoryBean proxyFactory = new JaxWsProxyFactoryBean();
proxyFactory.setServiceClass(CustomerWebService.class);
proxyFactory.setAddress("http://localhost:8080/services/customer");
return proxyFactory;
}
}
CustomerWebServiceImplTest
@ActiveProfiles(profiles = "test")
@ContextConfiguration(classes = {
PersistenceConfig.class,
RootConfig.class,
WebServiceConfig.class,
ClientConfig.class
})
@WebAppConfiguration
public class CustomerWebServiceImplTest {
private ApplicationContext context = new AnnotationConfigApplicationContext(ClientConfig.class);
private CustomerWebService customerWsProxy = (CustomerWebService) context.getBean("client");
@Test
public void addCustomer() {
CustomerDto customer = new CustomerDto();
customer.setName("John");
assertEquals("Hello " + customer.getName(), customerWsProxy.addCustomer(customer));
}
}
您能否提示错误可能出在哪里?
UPD:我在我和我的应用程序拥有完全访问权限的 PC 上检查了这个设置,它仍然抛出异常。
解决方案非常简单 - 只需添加 @RunWith(SpringRunner.class)
。因为这个注解是运行springbean,而不是@WebAppConfiguration
和@ContextConfiguration
。
这就是它的样子
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {
RootConfig.class,
WebServiceConfig.class,
ClientConfig.class
})
public class CustomerWebServiceImplTest {
...
}