什么是WebService?webService小示例 点此了解

下面进入正题:

Javaweb项目(spring项目)中集成webservice ,实现对外开放接口步骤:

准备:

采用与spring兼容性较好的cxf来实现

cxf 的  jar下载地址: http://cxf.apache.org/download.html

选择zip格式下载,解压后的lib目录下的jar

需要最少的jar如下:

cxf-2.3.3.jar
geronimo-annotation_1.0_spec-1.1.1.jar
geronimo-jaxws_2.2_spec-1.0.jar
geronimo-stax-api_1.0_spec-1.0.1.jar
geronimo-ws-metadata_2.0_spec-1.1.3.jar
jaxb-api-2.2.1.jar
jaxb-impl-2.2.1.1.jar
neethi-2.0.4.jar
wsdl4j-1.6.2.jar
XmlSchema-1.4.7.jar
wstx-asl-3.2.9.jar

一:创建webservice服务器

1)创建一个服务接口

  1. package com.service;
  2. import javax.jws.WebParam;
  3. import javax.jws.WebService;
  4. @WebService
  5. public interface IHelloWorld {
  6. public String sayHello(@WebParam(name = "arg0") String text);
  7. }

2)是接口实现类

  1. package com.service.impl;
  2. import javax.jws.WebService;
  3. import com.service.IHelloWorld;
  4. @WebService(endpointInterface = "com.service.IHelloWorld")
  5. public class HelloWorldImpl implements IHelloWorld {
  6. public String sayHello(String text) {
  7. return "Hello : " + text;
  8. }
  9. }

3)创建spring配置文件,将服务类加入到容器中

webservice.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:p="http://www.springframework.org/schema/p"
  5. xmlns:jaxws="http://cxf.apache.org/jaxws"
  6. xmlns:cxf="http://cxf.apache.org/core"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  9. http://cxf.apache.org/jaxws
  10. http://cxf.apache.org/schemas/jaxws.xsd">
  11. <import resource="classpath*:META-INF/cxf/cxf.xml" />
  12. <import resource="classpath*:META-INF/cxf/cxf-extension-soap.xml" />
  13. <import resource="classpath*:META-INF/cxf/cxf-servlet.xml" />
  14. <!--下面的class属性值一定要跟你项目中服务实现类的包路径完全一致-->
  15. <bean id="hello" class="com.service.impl.HelloWorldImpl"/>
  16. <jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" />
  17. </beans>

在web.xml中添加webservice.xml配置文件

  1. <context-param>
  2. <param-name>contextConfigLocation</param-name>
  3. <param-value>
  4. /WEB-INF/webservice.xml
  5. </param-value>
  6. </context-param>

4)在web.xml中加入cxf servlet

  1. <servlet>
  2. <display-name>CXF Servlet</display-name>
  3. <servlet-name>CXFServlet</servlet-name>
  4. <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  5. <load-on-startup>1</load-on-startup>
  6. </servlet>
  7. <servlet-mapping>
  8. <servlet-name>CXFServlet</servlet-name>
  9. <url-pattern>/webservice/*</url-pattern>
  10. </servlet-mapping>

至此,webservice服务器就创建好了,

在浏览器中访问:http://localhost:8080/test/webservice/HelloWorld?wsdl,(test是项目名称)。如果出现了类似与

<wsdl:definitions xmlns:ns1="http://service.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://impl.service.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="HelloWorldImplService" targetNamespace="http://impl.service.com/">
。。。。

就配置成功了。

接下来贴几个运行时的错误解决方法

1:webservice.xml中提示cxf.xml,cxf-servlet.xml not found,我在上面写的路径是classpath*:META-INF/cxf/cxf.xml,这里的classpath后面还跟了一个“*”符号,没加符号表示只在类路径下查找cxf.xml文件,加了号表示不仅在类路径下查找xml文件,还在jar包中查找xml文件。所以当我们在项目类路径下没有假如cxf.xml等配置文件时,就一定要在classpath后加*,这样spring容器才会去所加入的jar包中找。

2:假如cxf servlet时,映射路径不要写成/*,否则会出现访问不了项目主页的情况,可以写成/webservice/*或者别的项目中没有使用过的路径来作为cxf servlet的请求路径。

二:创建webservice客户端

客户端可以和服务器放在同一个项目中用来测试,也可以新建一个Java项目来进行测试。

新建一个Java项目测试时,要假如对应的jar包,跟服务器一样,使用spring还要假如spring jar包。

我在这里新建一个项目,依旧使用spring来测试

1)首先要创建一个和服务器端一样的服务接口,(如果客户端和服务器端在同一个项目中则可以省略这步)

  1. package com.service;
  2. import javax.jws.WebParam;
  3. import javax.jws.WebService;
  4. @WebService
  5. public interface IHelloWorld {
  6. public String sayHello(@WebParam(name = "arg0") String text);
  7. }

2)创建spring-client.xml,

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:cxf="http://cxf.apache.org/core"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  7. http://cxf.apache.org/jaxws
  8. http://cxf.apache.org/schema/jaxws.xsd">
  9. <bean id="client" class="com.service.IHelloWorld" factory-bean="clientFactory" factory-method="create" />
  10. <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
  11. <property name="serviceClass" value="com.service.IHelloWorld" />
  12. <property name="address" value="http://localhost:8080/test/HelloWorld" />
  13. </bean>
  14. </beans>

3)测试类

  1. package com.test;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. import com.service.IHelloWorld;
  5. public class Test {
  6. public static void main(String[] args) {
  7. ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-client.xml");
  8. IHelloWorld client = (IHelloWorld) ctx.getBean("client");
  9. String result = client.sayHello("你好!");
  10. System.out.println(result);
  11. }
  12. }

运行成功后显示

Hello:你好!

文章参考: http://www.open-open.com/lib/view/open1405929509210.html

java web项目(spring项目)中集成webservice ,实现对外开放接口的更多相关文章

  1. java web 手动部署项目步骤

    java Web 手动部署项目步骤 1 在tomcat下面的webapps下面建立需要部署的文件夹(eg:demo);2 在demo下建立 WEB-INF WETA-INF src 文件夹;3 在sr ...

  2. 细说shiro之五:在spring框架中集成shiro

    官网:https://shiro.apache.org/ 1. 下载在Maven项目中的依赖配置如下: <!-- shiro配置 --> <dependency> <gr ...

  3. spring boot 2 集成JWT实现api接口认证

    JSON Web Token(JWT)是目前流行的跨域身份验证解决方案.官网:https://jwt.io/本文使用spring boot 2 集成JWT实现api接口验证. 一.JWT的数据结构 J ...

  4. 传统Java Web(非Spring Boot)、非Java语言项目接入Spring Cloud方案

    技术架构在向spring Cloud转型时,一定会有一些年代较久远的项目,代码已变成天书,这时就希望能在不大规模重构的前提下将这些传统应用接入到Spring Cloud架构体系中作为一个服务以供其它项 ...

  5. 传统Java Web(非Spring Boot)、非Java语言项目接入Spring Cloud方案--temp

    技术架构在向spring Cloud转型时,一定会有一些年代较久远的项目,代码已变成天书,这时就希望能在不大规模重构的前提下将这些传统应用接入到Spring Cloud架构体系中作为一个服务以供其它项 ...

  6. java web:在eclipse中如何创建java web 项目

    Eclipse创建java web工程 eclipse版本:eclipse-jee-4.5-win32-x64 tomcat版本:apache-tomcat-7.0.63-windows-x64 jd ...

  7. Spring Boot中集成Spring Security 专题

    check to see if spring security is applied that the appropriate resources are permitted: @Configurat ...

  8. 使用Logstash同步数据至Elasticsearch,Spring Boot中集成Elasticsearch实现搜索

    安装logstash.同步数据至ElasticSearch 为什么使用logstash来同步,CSDN上有一篇文章简要的分析了以下几种同步工具的优缺点:https://blog.csdn.net/la ...

  9. JAVA学习3:Eclipse中集成Tomcat

    问题: 很多时候在Eclipse中启动Tmocat后,不能访问本机的localhost:8080主页,并且其他项目也不能访问. 原因: 打开Tomcat下的webapp后也找补到项目目录,这是因为Ec ...

随机推荐

  1. java课后思考题(五)

    1.使用Files. walkFileTree()找出指定文件夹下所有扩展名为.txt和.java的文件. import java.io.IOException;import java.nio.fil ...

  2. 机器学习框架ML.NET学习笔记【6】TensorFlow图片分类

    一.概述 通过之前两篇文章的学习,我们应该已经了解了多元分类的工作原理,图片的分类其流程和之前完全一致,其中最核心的问题就是特征的提取,只要完成特征提取,分类算法就很好处理了,具体流程如下: 之前介绍 ...

  3. SQL 索引查找

    索引查找信息 在非聚集索引里,会为每条记录存储一份非聚集索引索引键的值和一份聚集索引索引键 [在没有聚集索引的表格里,是RID值指向数据页面,有聚集索引的话指向聚集索引的键(在不使用include时) ...

  4. Java中的日志框架

    日志框架的介绍和使用 常见的日志框架:JUL(Java.util.logging),JCL(jakarta commons logging),SLF4J,jboss-logging,Log4j,Log ...

  5. 关于使用mybatis的分页插件问题

    首先我需要导入架包 1.pagehelper 如果你是在mybatis中配置分页‘ 如下代码 <plugins> <plugin interceptor="com.gith ...

  6. 用Meta标签代码让360双核浏览器默认极速模式打开网站不是兼容模式

    公司所作的页面在360下打开都会遇到在360下自动跳到360兼容模式引发许多兼容问题,摸索了好久终于在网上找到了怎么解决的方法,详情如下: 其实360给网站开发者设计了一种选择的方法,只要加入一段Me ...

  7. 微信成为HTML5技术流行的最大推手

    很多热点的事件都是厚积薄发,HTML5就是如此.此前iOS和Android系统已经放弃了Flash,这让HTML5有了一个天然的成长基础.而现在手机硬件的提升和HTML5本身的完善,使得基于HTML5 ...

  8. selenium-Python之上传文件

    对于web 页面的上传功能实现一般有一下两种方式 普通上传:普通的附件上传是将本地文件的路径作为一个值放在input标签中,通过form表单将这个值提交给服务器 插件上传:一般是指基于flash.ja ...

  9. 【UML】使用环境(转)

    http://blog.csdn.net/sds15732622190/article/details/49404169 用例图         用例图是在需求文档中使用的,但一定要配合用例一同使用. ...

  10. spring maven 包

    <spring-framework.version>.RELEASE</spring-framework.version> <dependency> <gro ...