Spring mvc + CXF Webservice/Restful Maven
来源:互联网 发布:linux i2c 设备驱动 编辑:程序博客网 时间:2024/06/05 04:01
我这里整合了webservice和restful,既支持soap格式也支持json格式;
1. pom.xml文件;
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com</groupId> <artifactId>springmvc</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>springmvc Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <spring.version>4.2.6.RELEASE</spring.version> <slf4j.version>1.7.7</slf4j.version> <log4j.version>1.2.17</log4j.version> </properties> <dependencies> <!-- 单元测试--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!-- spring核心包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>3.1.8</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.1.8</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxrs</artifactId> <version>3.1.8</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-jaxrs</artifactId> <version>1.9.13</version> </dependency> </dependencies></project>
2.在web.xml文件中配置引入Cxf配置文件
<?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 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!--加载配置文件--><context-param><param-name>contextConfigLocation</param-name><param-value> classpath:cxf/spring-cxf-*.xml </param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener> <!-- 防止spring内存溢出监听器 --> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <!--配置spring框架的Servlet--><servlet><servlet-name>appServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:servlet-context.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet> <servlet-mapping> <servlet-name>appServlet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> <!-- 配置CXF框架的Servlet --> <servlet> <servlet-name>CXFServlet</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/CXFServlet/*</url-pattern> </servlet-mapping> <!--拦截请求进行字符编码转换--> <filter> <filter-name>SetCharacterEncoding</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>SetCharacterEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file></welcome-file> </welcome-file-list></web-app>
3. spring-cxf-bean.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--restful webservice bean--> <bean id="personRestService" class="com.manager.webservice.restful.impl.PersonRestServiceImpl"/> <!--soap webservice bean--> <bean id="personSoapService" class="com.manager.webservice.soap.impl.PersonSoapServiceImpl"/></beans>
4.spring-cxf-soap.xml soap发布配置
<?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" xsi:schemaLocation="http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" default-lazy-init="true"> <!--soap接口--> <jaxws:server id="soapWebService" address="/soap"> <!--serviceBeans:暴露的WebService服务类--> <jaxws:serviceBean> <ref bean="personSoapService" /> </jaxws:serviceBean> </jaxws:server></beans>
PersonSoapService
package com.manager.webservice.soap;import com.manager.entity.person.Person;import javax.jws.WebService;import java.util.List;/** * Created by static on 2017/3/31. */@WebServicepublic interface PersonSoapService { public void createPerson(Person preson); public List<Person> queryPerson();}
PersonSoapServiceImpl
package com.manager.webservice.soap.impl;import com.manager.entity.person.Person;import com.manager.service.person.PersonService;import com.manager.webservice.soap.PersonSoapService;import javax.annotation.Resource;import javax.jws.WebService;import java.util.List;/** * Created by static on 2017/3/31. * soap风格接口 */@WebServicepublic class PersonSoapServiceImpl implements PersonSoapService { @Resource private PersonService personService; @Override public void createPerson(Person person) { personService.createPerson(person); } @Override public List<Person> queryPerson() { return personService.queryPerson(null); }}
5.spring-cxf-restful.xml json格式的配置
<?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:jaxrs="http://cxf.apache.org/jaxrs" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <jaxrs:server id="restfulWebService" address="/restful"> <!--<!–输入拦截器设置–> <jaxrs:inInterceptors> </jaxrs:inInterceptors> <!–输出拦截器设置–> <jaxrs:outInterceptors> </jaxrs:outInterceptors>--> <!--serviceBeans:暴露的WebService服务类--> <jaxrs:serviceBeans> <ref bean="personRestService"/> </jaxrs:serviceBeans> <!--对象转换--> <jaxrs:providers> <bean class="org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider"/> </jaxrs:providers> </jaxrs:server></beans>
PersonRestService
package com.manager.webservice.restful;import com.manager.entity.person.Person;import javax.ws.rs.*;import javax.ws.rs.core.MediaType;import java.util.List;/** * Created by static on 2017/3/31. */@Path("/person")@Produces(MediaType.APPLICATION_JSON)@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})// 声明支持的类型public interface PersonRestService { @POST @Path("createPerson") public void createPerson(Person person); @GET @Path("queryPerson") public List<Person> queryPerson(); @GET @Path("m_queryPerson") public List<Person> mQueryPerson();}
PersonRestServiceImpl
package com.manager.webservice.restful.impl;import com.manager.entity.person.Person;import com.manager.service.mong.MongodbService;import com.manager.service.person.PersonService;import com.manager.webservice.restful.PersonRestService;import javax.annotation.Resource;import java.util.List;/** * Created by static on 2017/3/31. * restful风格接口 */public class PersonRestServiceImpl implements PersonRestService{ @Resource private PersonService personService; @Resource private MongodbService mongodbService; @Override public void createPerson(Person person) { personService.createPerson(person); } @Override public List<Person> queryPerson() { return personService.queryPerson(null); } @Override public List<Person> mQueryPerson() { return mongodbService.queryPerson(); }}
6.运行访问:
示例下载:http://download.csdn.net/detail/u013772876/9809703
1 0
- Spring mvc + CXF Webservice/Restful Maven
- spring maven cxf jackson restful
- maven项目下使用webservice spring mvc + cxf
- maven+cxf编写RESTful风格webservice
- spring mvc - cxf webservice项目
- CXF+spring构建restful风格的webservice
- spring + springmvc + cxf 实现webservice restful
- Spring+CXF开发RESTful风格的WebService
- spring集成cxf 发布restful风格webservice
- spring整合cxf实现restful webservice
- Spring整合CXF webservice restful 实例
- Spring整合CXF webservice restful 实例
- Maven 集成CXF Webservice,Spring
- webservice cxf+spring maven项目
- Spring+CXF+Maven发布Webservice
- maven + spring + spring mvc + mybatis + cxf Demo
- Eclipse+Maven+Spring+CXF 构建webservice 服务
- maven+CXF+Spring+tomcat 开发webservice
- Android studio中导入外部项目
- 组队赛(F/D) UVALive
- CImagelist 基本用法
- 利用欧拉函数求N的正约数的个数
- Flex 之顶级常量和函数
- Spring mvc + CXF Webservice/Restful Maven
- CNN不能识别Negative图像
- iOS开发中 关于阿里云服务器的使用与安全策略
- mysql filesort
- (转)十一种通用滤波算法
- 1056. 组合数的和(15)
- LinkTextView-可点击的文本
- RTK无人机应用市场
- kmp字符串模式匹配中next函数值的算法