WebService体系之——与spring的整合

WebService体系之——与spring的整合

 

        摘要:好处意义什么的不再赘述、前面讲的东西也都是对webservice的认识、知道它基本的实现步骤、学习最终的目的就是应用、而spring在项目中的出镜率已经爆表、本篇笔记就是两者的整合的过程。

 

一:简介

 

        spring与webservice的整合很简单、如过对spring有所了解的话、不难猜想两者是如何结合的、我们使用spring的目的就是看重他的依赖注入功能、可以为我们通过配置文件自动生成bean、同时可以将我们指定的属性设置好、那么两者的整合肯定是通过配置spring的配置文件、让spring为我们生成项目运行中需要使用的关于webservice的bean、并且有我们想要的一切!

        1、新建web项目。

        2、引入jar包。

        3、修改web.xml。

        4、创建服务端webservice接口。

        5、实现服务端webservice接口实现类。

        6、新建spring的配置文件

        7、将项目发布到Tomcat服务器中、并启动。

        8、访问地址:有结果则说明webservice发布成功。

        9、新建测试类测试webservice发布的接口。

 

二:同一项目下具体的实现步骤

 

        1、创建web项目。右键。。新建。。。

        2、jar包的引入:前面提到过、下载下来的jar包中有一部分是spring的jar、当时没有过多提及、在这里就自然而然的想到是两者整合需要的jar包、我们直接将lib文件夹拷贝到项目的WEB-INFO下面即可。

        3、web.xml的配置:主要配置spring监听和配置文件加载位置以及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">
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:applicationContext-server.xml</param-value>
	</context-param>

	<!-- Spring 配置 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<listener>
		<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
	</listener>
	
	<!-- WebServices设置 -->  
    <servlet>  
        <servlet-name>CXFServices</servlet-name>  
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>  
        <load-on-startup>0</load-on-startup>  
    </servlet>  
    <servlet-mapping>  
        <servlet-name>CXFServices</servlet-name>  
        <url-pattern>/services/*</url-pattern>  
    </servlet-mapping>  
</web-app>

        4、创建服务端webservice接口——HelloService:


package com.chy.ws.service;

import javax.jws.WebService;

@WebService
public interface HelloService {
	public String sayHello(String name);
}

        5、实现服务端webservice接口实现类——HelloServiceImpl:


package com.chy.ws.service;

import javax.jws.WebService;

@WebService(endpointInterface="com.chy.ws.service.HelloServiceImpl")  
public class HelloServiceImpl implements HelloService {

	public String sayHello(String name) {
		return "hello " + name;
	}
}

        6、在web.xml中指定的spring配置文件的加载位置:src下新建applicationContext-server.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:jaxws="http://cxf.apache.org/jaxws"  
        xsi:schemaLocation="    
            http://www.springframework.org/schema/beans     
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
            http://cxf.apache.org/jaxws    
            http://cxf.apache.org/schemas/jaxws.xsd">  
            
    <!-- Import apache CXF bean definition 固定-->  
    <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" />  
      
    <!-- services接口配置 -->  
    <bean id="helloServicesBean" class="com.chy.ws.service.HelloServiceImpl" />  
    
    <!-- CXF 配置WebServices的服务名及访问地址 -->  
    <jaxws:server id="helloService" address="/HelloService" serviceClass="com.chy.ws.service.HelloService">  
            <jaxws:serviceBean>  
                <ref bean="helloServicesBean"/>  
            </jaxws:serviceBean>  
    </jaxws:server>  
</beans>  

        7、将项目发布到Tomcat服务器中、并启动。

        8、访问地址:http://localhost:8080/services  若有结果则服务发布成功。

        9、新建测试类——HelloServiceClient代码:


package com.chy.ws.service.client;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import com.chy.ws.service.HelloService;

public class HelloServiceClient {
	public static void main(String[] args) {
		invokByJava();
	}
	
	public static void invokByJava(){
		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
		factory.setServiceClass(HelloService.class);
		factory.setAddress("http://localhost:8080/webservice_spring_server/services/HelloService");
		HelloService helloService = (HelloService)factory.create();
		System.out.println(helloService.sayHello("andyChen"));
	}
}

 

三:不同项目下客户端具体的实现步骤

 

        1、实现服务端。

        2、创建web项目、引入jar包、和前面一样。

        3、配置web.xml——主要配置spring监听、配置文件的加载路径。

        4、创建与服务器端一样的接口(注意名称、包名也要一样、可以直接拷贝)——HelloService代码与服务端相同。

        5、spring配置文件applicationContext-client.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:jaxws="http://cxf.apache.org/jaxws"  
        xsi:schemaLocation="    
            http://www.springframework.org/schema/beans     
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
            http://cxf.apache.org/jaxws    
            http://cxf.apache.org/schemas/jaxws.xsd">  
    <!-- Import apache CXF bean definition -->  
    <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" />  
      
    <!-- CXF webservices 客户端配置 -->  
    <jaxws:client id="helloClient" serviceClass="com.chy.ws.service.HelloService"   
            address="http://localhost:8080/webservice_spring_server/services/HelloService">  
    </jaxws:client>  
</beans>  


        6、发布、启动。

        7、新建测试类:此时我们可以有两种选择:一种是使用原始的方式获取服务端、另一种是使用spring获取服务端——HelloServiceClient:

 

package com.chy.ws.service.test;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.chy.ws.service.HelloService;

public class HelloServiceClient {
	public static void main(String[] args) {
		invokByJava();
	}
	
	/**
	 * use spring's application context method to obtain web service.
	 */
	public static  void invokBySpring(){
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-client.xml");
		HelloService helloService = context.getBean("helloClient", HelloService.class);
		System.out.println(helloService.sayHello("andyChen"));
	}
	
	/**
	 * use original method to obtain web service.
	 */
	public static void invokByJava(){
		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
		factory.setServiceClass(HelloService.class);
		factory.setAddress("http://localhost:8080/webservice_spring_server/services/HelloService");
		HelloService helloService = (HelloService)factory.create();
		System.out.println(helloService.sayHello("andyChen"));
	}
}

四:补充

 

        完整项目图:


                

 

  • 10
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
SpringCloud Webservice 是一种基于SpringCloud框架的WebService接口的发布和调用方式。在SpringCloud中,我们可以使用SpringBoot来构建和发布WebService接口。通过使用SpringCloud的相关组件,我们可以实现不同服务之间的互联互通。 在具体实现上,我们可以使用SpringBoot的注解 @RestController 来标识一个类为WebService服务类,然后通过 @RequestMapping 来定义具体的接口路径和请求方式。通过这种方式,我们可以方便地创建和发布WebService接口。 同时,我们还可以使用SpringCloud的Feign来实现服务消费者的功能。Feign是一个声明式的Web服务客户端,它简化了服务间的调用过程。通过使用Feign,我们可以像调用本地方法一样调用远程服务的接口,从而实现服务之间的通信。 总结起来,SpringCloud Webservice 是通过SpringBoot和SpringCloud来构建和发布WebService接口,同时使用Feign来简化服务消费者的开发。这样可以方便地实现不同服务之间的互联互通。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [springboot/springcloud+webservice接口的发布](https://blog.csdn.net/A_dg_Jffery/article/details/102458070)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [springcloud feign 服务消费者 类似 webservice](https://download.csdn.net/download/knight_black_bob/9758389)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值