webService开发实例

本文详细介绍了如何使用Maven项目创建WebService服务端及客户端,包括添加依赖、创建接口与实现类、配置Spring与web.xml文件,以及客户端代码生成与测试过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一 前言

前面有一篇有关webService是转发他人的,这里自己整理一篇自己的案例,加深一下印象。话不多说,下面直接开始记录

二 webService服务端

1.创建一个maven项目

添加需要的依赖,pom文件如下所示:

<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.web</groupId>
  <artifactId>webserviceServer</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>webservice Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <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>2.5.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>2.5.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-bindings-soap</artifactId>
            <version>2.5.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>2.5.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-ws-security</artifactId>
            <version>2.5.0</version>
        </dependency>
  </dependencies>
  <build>
    <finalName>webserviceServer</finalName>
  </build>
</project>

2.新建webService接口以及其实现类

WeatherService:

package com.chp.webservice.server;

import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
public interface WeatherService {
	public String getWeather(@WebParam(name = "place") String place);
}

WeatherServiceImpl:

package com.chp.webservice.server;

import javax.jws.WebService;

@WebService(endpointInterface="com.chp.webservice.server.WeatherService")
public class WeatherServiceImpl implements WeatherService{

	@Override
	public String getWeather(String place) {
		
		return "天气晴朗";
	}

}

3.配置spring.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:jaxws="http://cxf.apache.org/jaxws" 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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
	<!--=============== 实现类的bean,需要spring注入 ============================ -->
	<bean id="weatherServiceImpl" class="com.chp.webservice.server.WeatherServiceImpl" />
	<jaxws:endpoint id="getWeather" implementor="#weatherServiceImpl" address="/getWeather" />
</beans>
 

4.配置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" version="2.5">
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/config/spring.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <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>
  <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>/service/*</url-pattern><!--路径/项目名/service/getWeather?wsdl -->
  </servlet-mapping>
  <display-name>hello world!</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
</web-app>

启动服务器,在浏览器输入地址:http://localhost:8080/webserviceServer/service/getWeather?wsdl

出现如下图代表新建成功

 

三 webService客户端

新建一个客户端webserviceClient的maven项目,右键新建webService的客户端

输入路径地址:http://localhost:8080/webserviceServer/service/getWeather?wsdl

就是服务端的访问路径

 

选择指定的位置

完成生成代码如下

测试:

public class TestService {
public static void main(String[] args) throws RemoteException {
	WeatherServiceProxy ws=new WeatherServiceProxy();
	String a=ws.getWeather("杭州");
	System.out.println(a);
}
}

输出结果则成功

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值