本人第一次写文章,各种工具使用还不熟悉,在编写过程中也出现不少问题。这里,不会深入到为什么,只是将用代码加文字描述把一个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
- <?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" 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">
- <display-name>cxf</display-name>
- <welcome-file-list>
- <welcome-file>index.html</welcome-file>
- <welcome-file>index.htm</welcome-file>
- <welcome-file>index.jsp</welcome-file>