cxf +spring 实现Webservice
第0步:User.java
package cxf;
/**
* 实体类
* @author LM
*/
public class User {
private Stringname;
private Stringage;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
第一步:HelloWorld.java
package cxf;
import java.util.List;
import javax.jws.WebParam;
import javax.jws.WebService;
/**
* 接口
* @author LM
*/
@WebService
public interface HelloWorld {
String sayHi(@WebParam(name="text")String text);
String sayHiToUser(User user);
String[] SayHiToUserList(List<User> userList);
}
第二步:HelloWorldImpl.java
package cxf;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* 实现类
*
* @author LM
*
*/
public class HelloWorldImplimplements HelloWorld {
Map<Integer, User> users =new LinkedHashMap<Integer, User>();
public String sayHi(String text) {
return "Hello " + text;
}
public String sayHiToUser(User user) {
users.put(users.size() + 1, user);
return "Hello " + user.getName();
}
public String[] SayHiToUserList(List<User> userList) {
String[] result = new String[userList.size()];
int i = 0;
for (User u : userList) {
result[i] = "Hello " + u.getName();
i++;
}
return result;
}
}
第四步:这是所有的jar包 下载地址
antlr-2.7.7.jar
aopalliance-1.0.jar
asm-3.3.1.jar
commons-beanutils-1.8.3.jar
commons-codec-1.4.jar
commons-collections-3.2.1.jar
commons-dbutils-1.5.jar
commons-httpclient.jar
commons-lang-2.6.jar
commons-lang3-3.0.1.jar
commons-logging-1.1.1.jar
cxf-2.6.3.jar
cxf-manifest.jar
cxf-services-sts-core-2.6.3.jar
cxf-services-wsn-api-2.6.3.jar
cxf-services-wsn-core-2.6.3.jar
cxf-xjc-boolean-2.6.0.jar
cxf-xjc-bug671-2.6.0.jar
cxf-xjc-dv-2.6.0.jar
cxf-xjc-runtime-2.6.0.jar
cxf-xjc-ts-2.6.0.jar
ehcache-core-2.5.1.jar
FastInfoset-1.2.12.jar
geronimo-activation_1.1_spec-1.1.jar
geronimo-annotation_1.0_spec-1.1.1.jar
geronimo-javamail_1.4_spec-1.7.1.jar
geronimo-jaxws_2.2_spec-1.1.jar
geronimo-jms_1.1_spec-1.1.1.jar
geronimo-servlet_2.5_spec-1.1.2.jar
geronimo-stax-api_1.0_spec-1.0.1.jar
geronimo-ws-metadata_2.0_spec-1.1.3.jar
h2-1.3.169.jar
isorelax-20030108.jar
jaxb-api-2.2.6.jar
jaxb-impl-2.2.5.jar
jaxb-xjc-2.2.5.jar
jettison-1.3.2.jar
jetty-continuation-7.5.4.v20111024.jar
jetty-http-7.5.4.v20111024.jar
jetty-io-7.5.4.v20111024.jar
jetty-security-7.5.4.v20111024.jar
jetty-server-7.5.4.v20111024.jar
jetty-util-7.5.4.v20111024.jar
joda-time-1.6.2.jar
js-1.7R2.jar
json-lib-2.4-jdk15.jar
jsr311-api-1.1.1.jar
mimepull-1.7.jar
msv-core-2011.1.jar
neethi-3.0.2.jar
oauth-20100527.jar
oauth-provider-20100527.jar
opensaml-2.5.1-1.jar
openws-1.4.2-1.jar
relaxngDatatype-20020414.jar
saaj-api-1.3.4.jar
saaj-impl-1.3.19.jar
serializer-2.7.1.jar
slf4j-api-1.6.2.jar
slf4j-jdk14-1.6.2.jar
spring-aop-3.0.7.RELEASE.jar
spring-asm-3.0.7.RELEASE.jar
spring-beans-3.0.7.RELEASE.jar
spring-context-3.0.7.RELEASE.jar
spring-core-3.0.7.RELEASE.jar
spring-expression-3.0.7.RELEASE.jar
spring-jms-3.0.7.RELEASE.jar
spring-tx-3.0.7.RELEASE.jar
spring-web-3.0.7.RELEASE.jar
stax2-api-3.1.1.jar
velocity-1.7.jar
woodstox-core-asl-4.1.4.jar
wsdl4j-1.6.2.jar
wss4j-1.6.7.jar
xalan-2.7.1.jar
xml-resolver-1.2.jar
xmlbeans-2.5.0.jar
xmlschema-core-2.0.3.jar
xmlsec-1.5.2.jar
xmltooling-1.3.2-1.jar
xsdlib-2010.1.jar
第五步:配置和spring配置文件applicationContext.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:p="http://www.springframework.org/schema/p"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
<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" />
<jaxws:endpoint id="sendServie" implementor="cxf.HelloWorldImpl"
address="/helloServie" />
</beans>
第五步:配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
<context-param>
<param-name>contextConfigLocation</param-name><param-value>classpath:/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Character Encoding filter -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>CXFServlet</servlet-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>/webservice/*</url-pattern>
</servlet-mapping>
</web-app>