我知道如何使用CXF创建基于REST的Web服务,并将数据自动反序列化为对象。但是如何使用CXF创建基于SOAP的Web服务?我需要对SOAP调用数据进行类似的反序列化到java对象。
提前致谢
答案 0 :(得分:2)
我找到了解决方案,现在就是这样......
<强>的pom.xml 强>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
<exclusions>
<exclusion>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
</exclusion>
<exclusion>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-servlet_2.5_spec</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<artifactId>cxf-rt-transports-http</artifactId>
<groupId>org.apache.cxf</groupId>
<version>${cxf.version}</version>
</dependency>
<强>的beans.xml 强>
<!-- xmlns:jaxws="http://cxf.apache.org/jaxws" http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd -->
<jaxws:endpoint address="/mysws"
implementor="com.company.project.MySoapService"
id="mySoapService" />
<强>爪哇强>
@WebService
public interface IMySoapService {
MySoapResponse someOperation(MySoapRequest requestObj);
}
@WebService(endpointInterface = "com.company.project.IMySoapService")
public class MySoapService implements IMySoapService {
public MySoapResponse someOperation(MySoapRequest requestObj) {
MySoapResponse myResponse = null;
try {
myResponse = new MySoapResponse("12345", "6789");
} catch (Exception ex) {
myResponse = new MySoapResponse(false, 234, "", "");
myResponse.setErrorMessage(ex.getMessage());
}
return myResponse;
}
}
希望能帮助有需要的人!
答案 1 :(得分:-1)
添加依赖
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.1</version>
</dependency>
添加web.xml
<web-app>
<display-name>CXF</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:ApplicationContext-cxf.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<display-name>CXF Servlet</display-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
添加应用程序上下文
<?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:jaxws="http://cxf.apache.org/jaxws" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:cxf="http://cxf.apache.org/core" xmlns:task="http://www.springframework.org/schema/task"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-extension-xml.xml"/>
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<task:annotation-driven />
<context:component-scan base-package="com.smoothexample">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Service" />
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Component" />
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Repository" />
</context:component-scan>
<context:annotation-config />
<context:component-scan base-package="com.smoothexample" />
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
<bean id="addressSoapService" class="com.smoothexample.cxf.soap.ws.impl.AddressSoapServiceImpl" />
<jaxws:endpoint id="addressEndpointId"
implementorClass="com.smoothexample.cxf.soap.ws.impl.AddressSoapServiceImpl"
implementor="#addressSoapService" address="/addressSoapService">
</jaxws:endpoint>
</beans>
Webservice
package com.smoothexample.cxf.soap.ws;
import javax.jws.WebService;
import com.smoothexample.dto.Address;
@WebService(endpointInterface = " com.smoothexample.cxf.soap.ws.AddressSoapService",
serviceName = "addressSoapService")
public interface AddressSoapService {
public Address getAddress()
throws Exception;
}
实施
package com.smoothexample.cxf.soap.ws.impl;
import com.smoothexample.cxf.soap.ws.AddressSoapService;
import com.smoothexample.dto.Address;
public class AddressSoapServiceImpl implements AddressSoapService {
public Address getAddress() {
return createAddress();
}
private Address createAddress() {
Address address = new Address();
address.setStreetAddress("4800 abc Rd");
address.setCity("abcd");
address.setState("NJ");
address.setCountry("US");
address.setZip("10001");
address.setAddressOptional("addressOptional");
return address;
}
}
请在http://www.smoothexample.com/webservices/apache_cxf_soap_web_services.html
了解更多详情