为什么 Web 服务和代理客户端未连接



我有一个应用程序,我尝试将Spring MVCApache CFX(肥皂(Web服务结合起来。 当我只运行应用程序时,一切似乎都很好,我看到通过这个链接(http://localhost:8080/services/customer?wsdl(生成的WSDL。但是当我运行测试时,它会抛出WebServiceException: Could not send Message... Connection refused .

我已经通过Windows Firewall Defender打开了公共,私有和域区域的所有端口。也许我错过了什么。为了调查它,我用这个命令检查了链接(wsimport -keep -verbose http://localhost:8080/services/customer?wsdl(。结果,它给出了这个:

[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;
    }
}

客户网络服务不合格测试

@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) .因为这个注解是运行春豆,而不是用@ContextConfiguration @WebAppConfiguration

这就是它的样子

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {
        RootConfig.class,
        WebServiceConfig.class,
        ClientConfig.class
})
public class CustomerWebServiceImplTest {
...
}

最新更新