【问题标题】:Configuration Apache CXF配置 Apache CXF
【发布时间】:2016-08-08 09:07:17
【问题描述】:

我尝试在我的项目中使用 Apache CXF。所以我设置了一个 xml 文件 cxf-client.xml 我把这段代码放在那里:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
   xsi:schemaLocation="http://cxf.apache.org/transports    /http/configuration
       http://cxf.apache.org/schemas/configuration/http-conf.xsd
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

       <import resource="classpath:META-INF/cxf/cxf.xml" />
       <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
       <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

       <http-conf:conduit name="*.http-conduit">
       <http-conf:client ConnectionTimeout="3000" ReceiveTimeout="3000"/>
       </http-conf:conduit>
</beans>

我的问题是如何以及在何处读取此文件以正确执行我的项目?我需要更多配置吗?

这是我的客户端类 java:

@WebServiceClient(name = "name", 
              wsdlLocation = "sourse?wsdl",
              targetNamespace = "myNameSpace") 
public class MyClass extends Service {

public final static URL WSDL_LOCATION;

public final static QName SERVICE = new QName("myNameSpace", "name");
public final static QName MyEndpointServiceImplPort = new QName("myNameSpace", "MyEndpointServiceImplPort");
static {
    URL url = null;
    try {
        String urlString = System.getProperty("webservice.trainmission.url");
        url = new URL(urlString);
    } catch (MalformedURLException e) {
        e.getMessage();
    }
    WSDL_LOCATION = url;
}

public MyClass(URL wsdlLocation, QName serviceName) {       
    super(wsdlLocation, serviceName);
}
@WebEndpoint(name = "MyEndpointServiceImplPort")
public MyEndpointServicePortType getMyEndpointServiceImplPort() {
    return super.getPort(MyEndpointServiceImplPort,    MyEndpointServicePortType.class);
}

这是我的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xmlns="http://java.sun.com/xml/ns/javaee"   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com /xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>MyProject</display-name>
<context-param>
  <param-name>contextConfigLocation</param-name>
<param-value>
        classpath:conf/cxf-client.xml
</param-value>
</context-param>
<servlet>
  <description>Apache CXF Endpoint</description>
  <display-name>cxf</display-name>
  <servlet-name>cxf</servlet-name>
  <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-  class>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>cxf</servlet-name>
  <url-pattern>/services/*</url-pattern>
</servlet-mapping>

<listener>
  <display-name>Spring Web Context Loader Listener</display-name>
  <listener-class>org.springframework.web.context.ContextLoaderListener
  </listener-class>
</listener>
</web-app>

当我创建MyClass 的实例时,它在super(wsdlLocation, serviceName); 这一行中被阻塞,并且它不使用我配置的超时时间

【问题讨论】:

  • 这是一个spring 配置文件。我建议你阅读一些关于 spring 的教程以了解如何在你的项目中使用它
  • 感谢您的回答!我在我的web.xml 中调用了这个类作为类路径。你知道我需要更多的配置还是很好?
  • 需要配置ContextLoaderListener。检查这个stackoverflow.com/questions/6451377/…
  • 我这样做就像我编辑了我的问题一样......你可以看到它!当我创建MyClass 的实例时,它在super(wsdlLocation, serviceName); 这一行中被阻塞,并且它不使用我配置的超时时间
  • 我很困惑。您的配置混合了服务器和客户端部分。您要发布服务器端点还是使用服务?

标签: java web-services cxf


【解决方案1】:

当构造函数尝试从 wsdlLocaltion 下载 WSDL 时,您的服务似乎被阻止了。 CXF 超时不适用于此处。确保 wsdlLocation 的 URL 是可访问的。如果没有

1) 指向本地 wsdl 文件。

File wsdlFile = new File(wsdl);
wsdlLocation = wsdlFile.toURI().toURL();

2) 在没有 WDSL 的情况下配置 CXF 网络服务

https://stackoverflow.com/a/19827446/6371459

QName qname = new QName("http://thenamespace", "FooService");
FooService service = new FooService(null, qname); // null for ignore WSDL
Foo port = service.getFooPort();
BindingProvider bindingProvider = (BindingProvider) port;
bindingProvider.getRequestContext()
   .put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
   "http://foo.com/soap/fooBean");

【讨论】:

  • 感谢您的回答!实际上,我找到了一个简单的解决方案,就像您在答案中看到的那样
  • 那么,你终于用BindingProvider配置了端点地址?
  • 抱歉,我无法发布我的答案!我不知道是什么问题!但告诉你,我使用了一个简单的解决方案。它是 URL 连接。在调用我的服务之前,我使用此解决方案来控制我的 URL:尝试 { URL testUrl = new URL(MyURL); URLConnection testConnection = testUrl.openConnection(); testConnection.setConnectTimeout(TIMEOUT_VALUE); testConnection.setReadTimeout(TIMEOUT_VALUE); } catch (SocketTimeoutException e) { System.out.println("超过 " + TIMEOUT_VALUE + " elapsed."); }
  • 好的,您已经为 wsdl 连接设置了超时。但是,如果无法下载 WDSL,则服务将不知道端点地址,并且服务将无法连接。不是吗?
  • 我为 wsdl 连接设置了超时。如果我无法连接到 wsdl 或者在连接后我无法读入 wsdl,我将 try bloc 留给异常..否则我执行 try bloc,我忘了在这里回答我的要求当我使用我的 wsdl 和端点作为参数时我的服务!所以我的控制是在调用我的服务之前
猜你喜欢
  • 2019-04-26
  • 2012-10-03
  • 1970-01-01
  • 2014-02-09
  • 2015-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-06
相关资源
最近更新 更多