木头人的gravatar头像
木头人 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,访问主页

spring mvc+spring整合cxf服务端和客户端之间访问(jaxws与jaxrs的使用)

7.调用jaxws发布的服务,通过cxf提供的wsdl2java工具生成代码

spring mvc+spring整合cxf服务端和客户端之间访问(jaxws与jaxrs的使用)

在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去访问主页

spring mvc+spring整合cxf服务端和客户端之间访问(jaxws与jaxrs的使用)

测试jaxrs服务,使用postman工具

spring mvc+spring整合cxf服务端和客户端之间访问(jaxws与jaxrs的使用)

spring mvc+spring整合cxf服务端和客户端之间访问(jaxws与jaxrs的使用)


打赏

文件名:webservice demo.zip,文件大小:75.755K 下载
最代码最近下载分享源代码列表最近下载
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日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友