分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 前端开发

webService之helloword

发布时间:2023-09-06 01:30责任编辑:傅花花关键词:word

webservice 远程数据交互技术

    1.导入jar包(如果是 maven项目导入项目坐标)

    2.创建服务

    3.测试服务

我们使用maven来做测试服务

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/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.baidu.webservicetest01</groupId><artifactId>webservicetest01</artifactId><version>0.0.1-SNAPSHOT</version><!-- jdk版本1.7 --><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>2.3.2</version><configuration><source>1.7</source><target>1.7</target></configuration></plugin></plugins></build><dependencies><!-- cxf ?ws开发包 --><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxws</artifactId><version>3.0.1</version></dependency><!-- jetty ?服务器包 --><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http-jetty</artifactId><version>3.0.1</version></dependency><!-- 使用log4j日志实现 --><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.12</version></dependency><!-- 使用rs客户端 --><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-rs-client</artifactId><version>3.0.1</version></dependency></dependencies></project>

  服务接口

package com.baidu.test;import javax.jws.WebMethod;import javax.jws.WebService;@WebServicepublic interface TestInterf {@WebMethodpublic void eat();}

服务实现类

package com.baidu.test;import javax.jws.WebService;@WebServicepublic class TestImp implements TestInterf {public void eat() {System.out.println("该吃饭了");}}

开启服务:

package com.baidu.test;import javax.xml.ws.Endpoint;public class PublishTest {public static void main(String[] args) {TestInterf tt=new TestImp();String address="http://localhost:9991/TestInterf";Endpoint.publish(address, tt);System.out.println("服务启动了");}}

新建项目  和服务器的包结构必须相同

测试项目的 测试接口和服务器的接口必须一致

package com.baidu.test1;import javax.jws.WebMethod;import javax.jws.WebService;@WebService ?//标识可以连接服务public interface TestInterf {@WebMethodpublic void eat();}

  

测试服务

package com.baidu.test1;import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;public class Test02 {public static void main(String[] args) {// 编写客户端 调用发布WebService服务JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();jaxWsProxyFactoryBean.setServiceClass(TestInterf.class);jaxWsProxyFactoryBean.setAddress("http://localhost:9991/TestInterf");// 创建调用远程服务代理对象TestInterf proxy = (TestInterf) jaxWsProxyFactoryBean.create();proxy.eat();}}

  pom.xml  客户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/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.baidu.webservicetest02</groupId><artifactId>webservicetest02</artifactId><version>0.0.1-SNAPSHOT</version><build> ???????<plugins> ???????????<plugin> ???????????????<groupId>org.apache.maven.plugins</groupId> ???????????????<artifactId>maven-compiler-plugin</artifactId> ???????????????<version>2.3.2</version> ???????????????<configuration> ???????????????????<source>1.7</source> ???????????????????<target>1.7</target> ???????????????</configuration> ???????????</plugin> ???????</plugins> ???</build><dependencies><!-- 要进行jaxws 服务开发 --> ?<dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxws</artifactId><version>3.0.1</version></dependency><!-- 内置jetty web服务器 ?--><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http-jetty</artifactId><version>3.0.1</version></dependency><!-- 日志实现 --><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.12</version></dependency></dependencies></project>

  

webService之helloword

原文地址:http://www.cnblogs.com/fjkgrbk/p/webService.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved