gpt4 book ai didi

java - 为什么 Web 服务和代理客户端无法连接?

转载 作者:行者123 更新时间:2023-12-02 02:40:19 25 4
gpt4 key购买 nike

我有一个应用程序,我尝试将 Spring MVCApache CFX(soap) Web 服务结合起来。当我只运行应用程序时,一切看起来都很好,我看到通过此链接生成的 WSDL(http://localhost:8080/services/customer?wsdl)。但是当我运行测试时,它抛出WebServiceException:无法发送消息...连接被拒绝

我已通过 Windows Firewall Defender 打开公共(public)、专用和域区域的所有端口。也许我错过了什么。在绝望地尝试调查它时,我使用此命令检查了链接(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/*");
}
}

Web服务配置

@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)。因为这个注解是运行spring beans的,而不是@WebAppConfiguration@ContextConfiguration

这就是它的样子

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

关于java - 为什么 Web 服务和代理客户端无法连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57182834/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com