首页 > 代码库 > (原创)Maven+Spring+CXF+Tomcat7 简单例子实现webservice
(原创)Maven+Spring+CXF+Tomcat7 简单例子实现webservice
这个例子需要建三个Maven项目,其中一个为父项目,另外两个为子项目
首先,建立父项目testParent,选择quickstart:
输入项目名称和模块名称,然后创建:
然后建立子项目testInterface:
再建立子项目projectImpl,过程跟上面以上,只是类型要选webapp(到时候启动工程或者打包都是以这个项目为主的):
建好之后,在testParent的pom.xml里面写入下面内容,主要是加入了cxf,junit,logback,springframework的依赖,在父项目加入依赖,子项目就不需要再加入依赖了:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>testParent</groupId> <artifactId>testParent</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <name>testParent</name> <url>http://maven.apache.org</url> <build> <finalName>testParent</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.2</version> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <org.springframework.version>3.2.8.RELEASE</org.springframework.version> <cxf.version>2.7.7</cxf.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-api</artifactId> <version>${cxf.version}</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-rs-security-cors</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-ws-policy</artifactId> <version>${cxf.version}</version> <type>jar</type> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-core</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxrs</artifactId> <version>${cxf.version}</version> </dependency> <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-rs-extension-providers</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <type>jar</type> <scope>provided</scope> </dependency> <dependency> <groupId>net.logstash.logback</groupId> <artifactId>logstash-logback-encoder</artifactId> <version>4.7</version> <exclusions> <exclusion> <artifactId>logback-classic</artifactId> <groupId>ch.qos.logback</groupId> </exclusion> <exclusion> <artifactId>logback-core</artifactId> <groupId>ch.qos.logback</groupId> </exclusion> <exclusion> <artifactId>jackson-databind</artifactId> <groupId>com.fasterxml.jackson.core</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${org.springframework.version}</version> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>3.2.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${org.springframework.version}</version> </dependency> </dependencies> <modules> <module>testInterface</module> <module>projectImpl</module> </modules> </project>
然后在testInterface的src/main/java下面新建类
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; @Path("/a") public interface TestIn1 { @GET @Path("/doGet/{first}/{last}") @Produces(MediaType.APPLICATION_XML) public String doGet(@PathParam(value="http://www.mamicode.com/first") String firstName,@PathParam(value="http://www.mamicode.com/last") String lastName); @POST @Path("/itemComfirm") @Produces(MediaType.APPLICATION_XML) public String itemComfirm(String xmlParam,@Context HttpServletRequest req,@Context HttpServletResponse res); }
然后在projectImpl的pom.xml修改,这里加入对testInterface子项目的依赖:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>testParent</groupId> <artifactId>testParent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>projectImpl</artifactId> <packaging>war</packaging> <dependencies> <dependency> <groupId>testParent</groupId> <artifactId>testInterface</artifactId> <version>122</version> <type>jar</type> <scope>compile</scope> </dependency> </dependencies> </project>
然后在projectImpl的src/main/java下面新建类,并且实现testInterface子项目中的TestIn1接口:
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class TestIn1Impl implements TestIn1 { public String doGet(String firstName, String lastName) { return "hihihihi"+","+firstName+","+lastName; } public String itemComfirm(String xmlParam, HttpServletRequest req, HttpServletResponse res) { return "hahahaha"+","+xmlParam; } }
再然后在下图路径修改application.xml和web.xml,没有文件的话就自己新建:
application.xml:
jaxrs:extensionMappings和jaxrs:languageMappings表示可根据URL后缀不同来进行不同的配置,例如/testURL.xml,那么该请求的请求头就会设置为application/xml,为/testURL.en,则会用en-gb来解析请求内容。详情可参考:http://cxf.apache.org/docs/jax-rs.html
<?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:jaxrs="http://cxf.apache.org/jaxrs" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd"> <bean id="TestIn1Impl" class="TestIn1Impl"/> <jaxrs:server id="TestIn1ImplServiceContainer"> <jaxrs:serviceBeans> <ref bean="TestIn1Impl" /> </jaxrs:serviceBeans> <jaxrs:extensionMappings> <entry key="xml" value="application/xml" /> </jaxrs:extensionMappings> <jaxrs:languageMappings> <entry key="en" value="en-gb"/> </jaxrs:languageMappings> </jaxrs:server> </beans>
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>fsp-api</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:application.xml</param-value> </context-param> <!-- spring context listener --> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- CXF --> <servlet> <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>/testParent/*</url-pattern> </servlet-mapping> </web-app>
然后再写一个测试类:
import javax.ws.rs.core.MediaType; import org.apache.cxf.jaxrs.client.WebClient; import org.junit.After; import org.junit.Before; import org.junit.Test; public class TestClient1 { private WebClient client; private String url="http://localhost:8080/testParent/testParent/a"; @Before public void setUp() throws Exception{ System.out.println("setupbegan"); client=WebClient.create(url).header("charset", "UTF-8").encoding("UTF-8").acceptEncoding("UTF-8"); } @After public void tearDown() throws Exception{ System.out.println("tearDownbegan"); client=null; } @Test public void testItemConfirm(){ System.out.println("began"); String xmlaaParam="<a>ddd</a>"; String responseMsg=client.path("itemComfirm").accept(MediaType.APPLICATION_XML).post(xmlaaParam,String.class); System.out.println("response :"+responseMsg); System.out.println("end"); } @Test public void testDoGet() { System.out.println("began"); String responseString = client.path("doGet/"+"dsdsd"+"/"+"uyuy"+"") .accept(MediaType.APPLICATION_XML) .get(String.class); System.out.println("response :"+responseString); System.out.println("end"); } }
最后,在Eclipse的sever插件中加入ProjetImpl项目,然后启动项目:
启动成功之后,执行测试类,控制台输出:
setupbegan
began
response :hihihihi,dsdsd,uyuy
end
tearDownbegan
setupbegan
began
response :hahahaha,<a>ddd</a>
end
tearDownbegan
如有错误,欢迎指出。
转载请注明出处:http://www.cnblogs.com/Starshot/p/6889751.html
(原创)Maven+Spring+CXF+Tomcat7 简单例子实现webservice