idea java web项目(spring项目)中集成webservice ,实现对外开放接口

4 篇文章 0 订阅
4 篇文章 0 订阅

参考:https://www.cnblogs.com/huangwentian/p/6813693.html


主要针对idea 修改了相关jar包


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

下面进入正题:

Java web项目(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


使用idea的jar包(********请仅仅使用这些jar包,删除所有其它与webservice相关的jar包,否则会报各种莫名其妙的错误,不信你去试试******)

 <cxf.version>3.2.1</cxf.version>


 <dependency>
          <groupId>org.apache.cxf</groupId>
          <artifactId>cxf-rt-frontend-jaxws</artifactId>
          <version>${cxf.version}</version>
      </dependency>
      <dependency>
          <groupId>org.apache.cxf</groupId>
          <artifactId>cxf-rt-transports-http</artifactId>
          <version>${cxf.version}</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-frontend-simple -->
      <dependency>
          <groupId>org.apache.cxf</groupId>
          <artifactId>cxf-rt-frontend-simple</artifactId>
          <version>${cxf.version}</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-wsdl -->
      <dependency>
          <groupId>org.apache.cxf</groupId>
          <artifactId>cxf-rt-wsdl</artifactId>
          <version>${cxf.version}</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-bindings-soap -->
      <dependency>
          <groupId>org.apache.cxf</groupId>
          <artifactId>cxf-rt-bindings-soap</artifactId>
          <version>${cxf.version}</version>
      </dependency>
      <!-- Jetty is needed if you're are not using the CXFServlet -->
      <dependency>
          <groupId>org.apache.cxf</groupId>
          <artifactId>cxf-rt-transports-http-jetty</artifactId>
          <version>${cxf.version}</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/wsdl4j/wsdl4j -->
      <dependency>
          <groupId>wsdl4j</groupId>
          <artifactId>wsdl4j</artifactId>
          <version>1.6.3</version>
      </dependency>

一:创建webservice服务器

1)创建一个服务接口

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

2)是接口实现类

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

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

webservice.xml

[html] view plain copy
 
  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.   
  12.     <import resource="classpath*:META-INF/cxf/cxf.xml" />  
  13.     <import resource="classpath*:META-INF/cxf/cxf-extension-soap.xml" />  
  14.     <import resource="classpath*:META-INF/cxf/cxf-servlet.xml" />  
  15.   
  16.     <!--下面的class属性值一定要跟你项目中服务实现类的包路径完全一致-->  
  17.     <bean id="hello" class="com.service.impl.HelloWorldImpl"/>  
  18.     <jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" />  
  19. </beans>  

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

[html] view plain copy
 
  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

[html] view plain copy
 
  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.   
  8. <servlet-mapping>  
  9.     <servlet-name>CXFServlet</servlet-name>  
  10.     <url-pattern>/webservice/*</url-pattern>  
  11. </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)首先要创建一个和服务器端一样的服务接口,(如果客户端和服务器端在同一个项目中则可以省略这步)

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

2)创建spring-client.xml,

[html] view plain copy
 
  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.   
  10.     <bean id="client" class="com.service.IHelloWorld" factory-bean="clientFactory" factory-method="create" />  
  11.     <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">  
  12.         <property name="serviceClass" value="com.service.IHelloWorld" />  
  13.         <property name="address" value="http://localhost:8080/test/HelloWorld" />  
  14.     </bean>  
  15. </beans>  

3)测试类

 

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

 

运行成功后显示

Hello:你好!

 

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring项目,可以通过使用Spring Web Services框架来创建Web服务。在返回指定格式数据方面,可以使用Spring的消息转换器(Message Converters)来实现。 以下是一个示例代码,演示如何使用Spring Web Services框架和消息转换器来返回JSON格式的数据: ```java import org.springframework.http.MediaType; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class MyWebService { @RequestMapping(value = "/my-webservice", produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public MyResponseObject myWebServiceMethod() { MyResponseObject response = new MyResponseObject(); // populate response object return response; } } ``` 在上面的示例代码,我们使用 @RequestMapping 注解来映射一个Web服务方法。我们指定了 produces 参数来指定响应的内容类型为 JSON。我们还使用 @ResponseBody 注解来指示Spring将返回值转换为JSON格式并作为响应体发送。 需要注意的是,要使用消息转换器,需要在Spring的配置文件添加以下配置: ```xml <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes" value="application/json" /> </bean> </list> </property> </bean> ``` 在上面的配置,我们添加了一个用于转换JSON格式的消息转换器。这个转换器将会被RequestMappingHandlerAdapter自动识别,并用于将响应转换为JSON格式。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值