木头人
2017-05-25 14:42:39
原
spring mvc+spring整合cxf服务端和客户端之间访问(jaxws与jaxrs的使用)
1.构建自己的maven项目需要的jar
<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.jbw</groupId> <artifactId>WebServiceDemoAndSpringMVC</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>WebServiceDemoAndSpringMVC Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <cxf.version>3.0.4</cxf.version> <spring.version>4.1.5.RELEASE</spring.version> </properties> <dependencies> <!-- soap service --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>${cxf.version}</version> </dependency> <!-- restful service --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxrs</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>javax.ws.rs</groupId> <artifactId>jsr311-api</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>${cxf.version}</version> </dependency> <!-- spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</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-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</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-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> <!-- 使用gson格式化 --> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.7</version> </dependency> <!-- 使用jackson --> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-jaxrs</artifactId> <version>1.9.13</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-xc</artifactId> <version>1.9.13</version> </dependency> <!-- Other dependencies --> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.1</version> </dependency> </dependencies> <build> <finalName>WebServiceDemoAndSpringMVC</finalName> </build> </project>
2.构建自己服务端(service层)--jaxws
package com.jbw.service.webservice.impl; import javax.jws.WebService; import com.jbw.model.Order; import com.jbw.service.webservice.OrderWebService; @SuppressWarnings("restriction") @WebService public class OrderWebServiceImpl implements OrderWebService { public OrderWebServiceImpl(){ System.out.println("OrderWebServiceImpl"); } public Order getOrderbyId(int id) { System.out.println("GetOrderById's Id : "+id); return new Order(id, "Mr.Jiang's car", 222222); } }
构建自己服务端(service层)--jaxrs
package com.jbw.service.webservice.impl; import java.util.ArrayList; import java.util.List; import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import com.jbw.model.Category; import com.jbw.service.webservice.CategoryService; @Path("category") public class CategoryServiceImpl implements CategoryService { List<Category> result; public void init(){ result= new ArrayList<Category>(); result.add(new Category(1, "第一个分类")); result.add(new Category(2, "第二个分类")); } @GET @Consumes({ MediaType.APPLICATION_FORM_URLENCODED }) @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) public List<Category> getCategories() { init(); return result; } @PUT @Path("add") @Produces("text/html") @Consumes( { "application/xml", "application/json" }) public String addCategory(Category category) { System.out.println("处理添加类别逻辑,接受的数据为id:"+category.getCategoryId()+",name:"+category.getCategoryName()); return "ok"; } @GET @Path("query/{categoryId}") @Produces({"application/json","application/xml"}) public Category getCategory(@PathParam("categoryId") int id){ init(); for(Category category:result){ if(category.getCategoryId()==id){ return category; } } return null; } }
3.通过spring发布自己的服务
<?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" xmlns:cxf="http://cxf.apache.org/core" 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 http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd"> <!-- 引cxf的一些核心配置 --> <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" /> --> <!-- 配置方式1: --> <jaxws:server id="orderWebService1" serviceClass="com.jbw.service.webservice.OrderWebService" address="/orderWs1"> </jaxws:server> <!-- 配置方式2:soap service implementor指的是实现类 --> <jaxws:endpoint id="orderWebService2" implementor="com.jbw.service.webservice.impl.OrderWebServiceImpl" address="/orderWs"> <jaxws:inInterceptors> <bean class="org.apache.cxf.interceptor.LoggingInInterceptor" /> </jaxws:inInterceptors> <jaxws:outInterceptors> <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor" /> </jaxws:outInterceptors> </jaxws:endpoint> <!-- 配置全局拦截器 --> <cxf:bus> </cxf:bus> <!-- restful service --> <bean id="categoryService" class="com.jbw.service.webservice.impl.CategoryServiceImpl" /> <jaxrs:server id="categoryWS" address="/category"> <jaxrs:serviceBeans> <ref bean="categoryService" /> </jaxrs:serviceBeans> <jaxrs:invoker> </jaxrs:invoker> <!-- 捆绑多个拦截器,不用一一罗列 --> <jaxrs:features> <bean class="com.jbw.features.MyFeatures"></bean> </jaxrs:features> <jaxrs:inInterceptors> <bean class="org.apache.cxf.interceptor.LoggingInInterceptor" /> </jaxrs:inInterceptors> <jaxrs:outInterceptors> <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor" /> </jaxrs:outInterceptors> <!--支持的协议 --> <jaxrs:extensionMappings> <entry key="json" value="application/json" /> <entry key="xml" value="application/xml" /> </jaxrs:extensionMappings> <jaxrs:providers> <!-- 返回json数据 --> <bean class="org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider" /> </jaxrs:providers> </jaxrs:server> </beans>
4.使用springmvc
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- inject --> <mvc:annotation-driven /> <!-- 把标记了@Controller注解的类转换为bean --> <context:component-scan base-package="com.jbw" /> <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/view/" p:suffix=".jsp" /> </beans>
5.配置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>WebServiceDemoAndSpringMVC</display-name> <!-- 配置beans.xml --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:beans.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <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>/*</url-pattern> </servlet-mapping> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
6.开启Tomcat,访问主页
7.调用jaxws发布的服务,通过cxf提供的wsdl2java工具生成代码
在controller注入service
package com.cn.controller; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.jbw.service.webservice.Order; import com.jbw.service.webservice.OrderWebService; @Controller @RequestMapping("/order") public class OrderController { @Autowired private OrderWebService orderWebService; @RequestMapping("/queryOrderById") public String queryOrderById(int id,HttpServletRequest request){ Order order=orderWebService.getOrderbyId(id); request.setAttribute("order",order); return "orderinfo"; } }
运行客户端,开启Tomcat去访问主页
测试jaxrs服务,使用postman工具
猜你喜欢
- cxf整合spring实现java webservice接口示例
- Spring整合apache CXF发布RESTfull风格的java WebService接口
- spring整合rmi代码实例
- Spring整合DWR实例
- httpclient4.x调用cxf发布的webservice的某个方法(有参数,有返回值)(未整合spring)
- OAuth1 OAuth2 OPenid 整合spring带源码
- resteasy和spring整合开发restful风格接口的案例分享
- spring + cxf 的webservice服务端和客户端功能
- spring整合dwr的入门学习代码实例
- spring mvc+spring+mybatis+maven+easyui项目整合代码demo
- Spring整合Quartz实现分布式集群实例
- Spring整合Mybatis实现动态数据源切换教程配置
请下载代码后再发表评论
文件名:webservice demo.zip,文件大小:75.755K
下载
- /
- /WebServiceDemo
- /WebServiceDemoAndSpringMVC
- /WebServiceDemoAndSpringMVC/.classpath
- /WebServiceDemoAndSpringMVC/.project
- /WebServiceDemoAndSpringMVC/.settings
- /WebServiceDemoAndSpringMVC/.settings/.jsdtscope
- /WebServiceDemoAndSpringMVC/.settings/org.eclipse.jdt.core.prefs
- /WebServiceDemoAndSpringMVC/.settings/org.eclipse.m2e.core.prefs
- /WebServiceDemoAndSpringMVC/.settings/org.eclipse.wst.common.component
- /WebServiceDemoAndSpringMVC/.settings/org.eclipse.wst.common.project.facet.core.prefs.xml
- /WebServiceDemoAndSpringMVC/src
- /WebServiceDemoAndSpringMVC/src/main
- /WebServiceDemoAndSpringMVC/src/main/java
- /WebServiceDemoAndSpringMVC/src/main/java/com
- /WebServiceDemoAndSpringMVC/src/main/java/com/jbw
- /WebServiceDemoAndSpringMVC/src/main/java/com/jbw/features
- /WebServiceDemoAndSpringMVC/src/main/java/com/jbw/intercepter
- /WebServiceDemoAndSpringMVC/src/main/java/com/jbw/jsonProvider
- /WebServiceDemoAndSpringMVC/src/main/java/com/jbw/model
- /WebServiceDemoAndSpringMVC/src/main/java/com/jbw
- /WebServiceDemoAndSpringMVC/src/main/java/com
- /WebServiceDemoAndSpringMVC/src/main/java
- /WebServiceDemoAndSpringMVC/src/main
- /WebServiceDemo
相关代码
- 证精 基于浏览器首选语言的springmvc和freemarker国际化配置的实现
- 原 基于maven与springMVC的拦截器Interceptor,控制器Controller的使用
- springmvc @RequestMapping注解的入门配置教程
- 精 SpringMVC的三种统一异常处理实例代码分享
- 证精 bootstrap jquery underscore backbone 新浪微博 springmvc整合demo代码
- 精 spring mvc-showcase,便于学习springmvc,maven工程,建议使用谷歌浏览器
- 原证精 springmvc @RequestMapping注解伪静态技术
- 原 springMVC注解与hibernate完美结合完成基本的增删改查demo源码下载
- 原 SpringMVC整合Redis demo小例子
- 原证精 java Web SpringMVC项目实现12306余票查询功能
- 原证 springmvc开发微信公众平台验证token接口教程及其java源代码分享
- 原 SpringMvc整合Mybatis搭建java Web入门项目实例,是基于MAVEN的java web工程
最近下载
liweiyishuo LV3
2021年12月2日
miaozhenyu LV2
2020年4月9日
eddxbb LV3
2019年9月9日
wsupsup LV16
2019年8月12日
wbbhappy LV13
2019年2月21日
zhu6272051 LV1
2019年2月20日
jywdream LV11
2019年2月13日
x646936674 LV10
2018年10月20日
67806686 LV1
2018年7月20日
ccccjb LV3
2018年6月8日
最近浏览
我睡觉时不困 LV7
2022年11月19日
微信网友_6013757501329408 LV1
2022年6月22日
gaotieyou LV5
2022年4月11日
liweiyishuo LV3
2021年12月2日
郭亚鹏 LV12
2021年8月10日
一朝两卜視
2021年8月10日
暂无贡献等级
329512801 LV28
2021年6月23日
GameName LV2
2021年6月7日
gao123qq LV21
2021年5月7日
haishen LV8
2021年4月11日