加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

使用cxf实现webService服务

发布时间:2020-12-16 22:20:03 所属栏目:安全 来源:网络整理
导读:本人第一次写文章,各种工具使用还不熟悉,在编写过程中也出现不少问题。这里,不会深入到为什么,只是将用代码加文字描述把一个webService的创建到使用以及与spring的整合叙述一下,也希望大神们多多指教。 1.首先看下webService的目录结构。 1)HelloWorld

本人第一次写文章,各种工具使用还不熟悉,在编写过程中也出现不少问题。这里,不会深入到为什么,只是将用代码加文字描述把一个webService的创建到使用以及与spring的整合叙述一下,也希望大神们多多指教。

1.首先看下webService的目录结构。

1)HelloWorldClient这个是用于前期测试使用。后面会从新创建一个项目作为客户端调用service。

2)pojo,实体类,这个相信有一点基础的人都知道是干嘛的,不再叙述。

3)webServiceApp用于前期未引入spring进行测试。

4)service层的接口与实现。

5)applicationContext.xml 未引入spring 前不使用。



2.下面开始具体描述。

1)新建java web工程 之后,下载cxf工具包。解压CXF之后,把cxf工具包lib下的jar包全部放到工程的lib下。可以去官方下载,下载地址:http://cxf.apache.org/download.html。

2)创建User.java

------------------------------------------------------------------------------------------

package com.jian.pojo;
import java.io.Serializable;
@SuppressWarnings("serial")
public class User implements Serializable {
private String id;
private String name;
private String age;
private String description;
? ? ? //此处省略get,set ,toString方法
}

-------------------------------------------------------------------------------------------

3)创建接口HelloWorld.java

-------------------------------------------------------------------------------------------

package com.jian.service;
import java.util.List;
import javax.jws.WebParam;
import javax.jws.WebService;
import com.jian.pojo.User;
@WebService ?
public interface HelloWorld {
? ?String sayHi(@WebParam(name="text")String text); ?
? ?String sayHiToUser(User user); ?
? ?String[] SayHiToUserList(List<User> userList); ?
}

-----------------------------------------------------------------------------------------

4)service接口实现?HelloWorldImpl.java

-----------------------------------------------------------------------------------------

package com.jian.service.impl;
import java.util.List;
import javax.jws.WebService;
import com.jian.pojo.User;
import com.jian.service.HelloWorld;
@WebService(endpointInterface = "com.jian.service.HelloWorld",serviceName = "helloWorld") ? //这里注意com.jian.service.HelloWorld不少情况下容易弄错
public class HelloWorldImpl implements HelloWorld {
public String sayHi(String text) {
return "hello" + text;
}
public String sayHiToUser(User user) {
return "hello " + user.getName() + user.getDescription();
}
public String[] SayHiToUserList(List<User> userList) {
String[] result = new String[userList.size()];
int i = 0;
for (User u : userList) {
result[i] = "Hello " + u.getName();
i++;
}
return result;
}
}

---------------------------------------------------------------------------------------------------------------------------

5)接下来可以编写服务端启动代码了webServiceApp.java

---------------------------------------------------------------------------------------------------------------------------

package com.jian.server;
import javax.xml.ws.Endpoint;
import com.jian.service.impl.HelloWorldImpl;

public class webServiceApp { ?
??
? ? public static void main(String[] args) { ?
? ? ? ? ?System.out.println("web service start"); ?
? ? ? ? ?HelloWorldImpl implementor = new HelloWorldImpl(); ?
? ? ? ? ?String address = "http://localhost:8080/cxfWebService"; ?
? ? ? ? ?Endpoint.publish(address,implementor); ?
? ? ? ? ?System.out.println("web service started"); ?
? ? } ?
??
} ?

-----------------------------------------------------------------------------------------------------

6)在编写客户端代码 ?HelloWorldClient .java

-----------------------------------------------------------------------------------------------------

package com.jian.client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import com.jian.pojo.User;
import com.jian.service.HelloWorld;

public class HelloWorldClient {
public static void main(String[] args) {
//首先右键run as 运行com.jian.server.webServiceApp类,然后再运行这段客户端代码 ?
JaxWsProxyFactoryBean jwpfb = new JaxWsProxyFactoryBean(); ?
jwpfb.setServiceClass(HelloWorld.class);
jwpfb.setAddress("http://localhost:8080/cxfWebService"); ?
HelloWorld hw = (HelloWorld) jwpfb.create(); ?
User user = new User(); ?
user.setName("马克思"); ??
user.setDescription("怀念马克思"); ?
System.out.println(hw.sayHiToUser(user));?
}

? ?}

--------------------------------------------------------------------------------------------------------

7)到这里,一个未整合spring的webService已经完成了。先启动webServiceApp,启动后可以看到如下图,说明启动正常



然后再启动?HelloWorldClient ,如果未报错,可以看到结果:



8)接下来,整合spring。

n1:配置web.xml

  1. <?xml?version="1.0"?encoding="UTF-8"?>??
  2. <web-app?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"?xmlns="http://java.sun.com/xml/ns/javaee"?xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"?xsi:schemaLocation="http://java.sun.com/xml/ns/javaee?http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"?id="WebApp_ID"?version="2.5">??
  3. ??display-name>cxf</>??
  4. ??welcome-file-list ????welcome-file>index.html ????>index.htm>index.jsp>default.html>default.htm>default.jsp ????
  5. context-param ????????param-name>contextConfigLocation ????????param-value>WEB-INF/classes/applicationContext.xml ??
  6. listenerlistener-class ??????????????org.springframework.web.context.ContextLoaderListener??
  7. servletservlet-name>CXFServletservlet-class ???????????????org.apache.cxf.transport.servlet.CXFServlet??
  8. load-on-startup>1servlet-mapping ????????? ?????????url-pattern>/* ????
  9. ??<!--?字符过滤器?-->????
  10. filter>????
  11. filter-name>encoding>????
  12. filter-class>org.springframework.web.filter.CharacterEncodingFilterinit-param ???????????? ????????????>UTF-8>forceEncoding>true ????????
  13. ????????
  14. filter-mapping>*.jsp>*.html>*.do>*.action>???
  15. >*.3g>?????
  16. web-app>??

n2:?配置applicationContext.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.xsd ?
? ? ? ? ? ? ?http://cxf.apache.org/jaxws ??
? ? ? ? ? ? ?http://cxf.apache.org/schemas/jaxws.xsd">
<!-- <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" />
?-->
<jaxws:endpoint id="helloWorld" implementor="com.jian.service.impl.HelloWorldImpl" address="/helloWorld" />
<bean id="client" class="com.jian.service.HelloWorld" factory-bean="clientFactory" factory-method="create" />
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="com.jian.service.HelloWorld" />
<property name="address" value="http://localhost:8080/cxfWebService" />
</bean>
</beans> ?

-------------------------------------------------------------------------------------------------------------------

n3:修改测试 ?HelloWorldClient?

-------------------------------------------------------------------------------------------------------------------

package com.jian.client;
import java.util.ArrayList;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.jian.pojo.User;
import com.jian.service.HelloWorld;

public class HelloWorldClient {
public static void main(String[] args) {
? ? ApplicationContext factory = new ClassPathXmlApplicationContext("/applicationContext.xml"); ?
? ? ? ?HelloWorld client = (HelloWorld)factory.getBean("client"); ?
? ? ? ?User user1 = new User(); ?
? ? ? ?user1.setName("马克思"); ?
? ? ? ?user1.setDescription("怀念马克思"); ?
? ? ? ?User user2 = new User(); ?
? ? ? ?user2.setName("恩格斯"); ?
? ? ? ?user2.setDescription("怀念恩格斯"); ?
? ? ? ?List<User> userList= new ArrayList<User>(); ?
? ? ? ?userList.add(user1); ?
? ? ? ?userList.add(user2);?
? ? ? ?System.out.println( client.sayHiToUser(user1));

-------------------------------------------------------------------------------------------------------------------------------------------

运行后发现报错。但是今天运行了下又没有报错,不知道问题出在哪里了。

如果报错的话可以执行一下操作。


个人寻找到的原因是该项目不是webservice的客户端要无法接下xml格式文件,这时,可以重新创建一个新的项目作为客户端。如下:

new-->other --webServiceClient


然后点击create a new web service project?

创建完成后会出先如下图:


填写wsdl url


然后next。。。就OK了,客户端会获取到service端的相关内容。

在这里可以做测试了

测试小例子:test.java

-------------------------------------------------------------------------------------------------------------------------

import com.jian.service.SayHiToUser;
import com.jian.service.User;

public class test {
? ?public static void main(String[] args) {
? User u = new User();
? u.setDescription("helloworld");
? u.setName("马克思");
? SayHiToUser he = new SayHiToUser();
? he.setArg0(u);
?System.out.println(he.getArg0().getName() + he.getArg0().getDescription()) ?;
? ?}
}

------------------------------------------------------------------------------------------------------------------------------------

输出结果


这样,一个cxf 与spring的服务器端与客户端的程序就结束了。


参考的文档:http://blog.csdn.net/hu_shengyang/article/details/38384597

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读