(2)配置文件说明
applicationContext.xml文件的内容如下:
view plaincopy to clipboardprint?
<?xml version="1.0" encoding="UTF-8"?>
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
services.xml文件的内容如下:
view plaincopy to clipboardprint?
<?xml version="1.0" encoding="UTF-8"?>
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">
address="/HelloWorld" implementor="com.cxf.test.interfaces.HelloWorldImpl"/>
web.xml文件的内容如下:
view plaincopy to clipboardprint?
<?xml version="1.0" encoding="UTF-8"?>
cxfservice
contextConfigLocation
WEB-INF/classes/applicationContext.xml
org.springframework.web.context.ContextLoaderListener
CXFServlet
org.apache.cxf.transport.servlet.CXFServlet
CXFServlet
/services/*
3)发布的HelloWord服务说明
要发布的HelloWorld服务的接口定义文件com.cxf.test.interfaces.HelloWorld:
package com.cxf.test.interfaces;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
@WebService
public interface HelloWorld
{
/*
- 一个简单的方法,返回一个字符串
- @param hello
-
@return
*/
String say(String hello);/**
稍微复杂一些的方法,传递一个对象给服务端处理
@param user
-
@return
*/
String sayUserName(@WebParam(name = "user") UserDTO user);/**
最复杂的方法,返回一个List封装的对象集合
-
@return
*/
public @WebResult(partName = "o") ListObject findUsers();
}要发布的HelloWorld服务的接口实现类com.cxf.test.interfaces.HelloWorldImpl:
view plaincopy to clipboardprint?
package com.cxf.test.interfaces;
import java.util.ArrayList;
import javax.jws.WebService;
/**
- @author zhangzk
-
/
/*
- WebService实现类.
-
使用@WebService指向Interface定义类即可.
*/
@WebService(endpointInterface = "com.cxf.test.interfaces.HelloWorld")
public class HelloWorldImpl implements HelloWorld
{
public String sayUserName(UserDTO user)
{
return "hello " + user.getName();
}public String say(String hello)
{
return "hello " + hello;
}public ListObject findUsers()
{
ArrayList list = new ArrayList();list.add(instancUser(1, "lib")); list.add(instancUser(2, "mld")); list.add(instancUser(3, "lq")); list.add(instancUser(4, "gj")); ListObject o = new ListObject(); o.setList(list); return o;
}
private UserDTO instancUser(Integer id, String name)
{
UserDTO user = new UserDTO();
user.setId(id);
user.setName(name);
return user;
}
}
findUsers()接口返回的参数对象定义文件com.cxf.test.interfaces.ListObject:
view plaincopy to clipboardprint?
package com.cxf.test.interfaces;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "listObject", propOrder ={ "list" })
public class ListObject
{
@XmlElement(nillable = true)
protected List list;
/**
* Gets the value of the list property.
*
* <p>
* This accessor method returns a reference to the live list, not a snapshot. Therefore any modification you make to the returned list will be
* present inside the JAXB object. This is why there is not a <CODE>set</CODE> method for the list property.
*
* <p>
* For example, to add a new item, do as follows:
*
* <pre>
* getList().add(newItem);
* </pre>
*
*
* <p>
* Objects of the following type(s) are allowed in the list {@link Object }
*
*
*/
public List<Object> getList()
{
if (list == null)
{
list = new ArrayList<Object>();
}
return this.list;
}
public void setList(ArrayList<Object> list)
{
this.list = list;
}
}
UserDTO instancUser(Integer id, String name)接口返回的对象定义文件com.cxf.test.interfaces.UserDTO:
view plaincopy to clipboardprint?
package com.cxf.test.interfaces;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;
/**
- Web Service传输User信息的DTO.
- 分离entity类与web service接口间的耦合,隔绝entity类的修改对接口的影响. 使用JAXB 2.0的annotation标注JAVA-XML映射,尽量使用默认约定.
-
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "User")
public class UserDTO
{protected Integer id;
protected String name;
public Integer getId()
{
return id;
}public void setId(Integer value)
{
id = value;
}public String getName()
{
return name;
}public void setName(String value)
{
name = value;
}
}
http://127.0.0.1:8080/cfx_spring_webservice/services/HelloWorld?wsdl 得出的结果为
<?xml version="1.0" encoding="UTF-8" ?>
- wsdl:types
- /xs:complexType
- xs:sequence /xs:sequence /xs:complexType
- xs:sequence /xs:sequence /xs:complexType
- xs:sequence /xs:sequence /xs:complexType
- xs:sequence /xs:sequence /xs:complexType
- xs:sequence /xs:sequence /xs:complexType
- xs:sequence /xs:sequence /xs:complexType
- xs:sequence /xs:sequence /xs:complexType /xs:schema /wsdl:types
- /wsdl:message
- /wsdl:message
- /wsdl:message
- /wsdl:message
- /wsdl:message
- /wsdl:message
- /wsdl:operation
- /wsdl:operation
- /wsdl:operation /wsdl:portType
- /wsdl:input
- /wsdl:output /wsdl:operation
- /wsdl:input
- /wsdl:output /wsdl:operation
- /wsdl:input
- /wsdl:output /wsdl:operation /wsdl:binding
- /wsdl:port /wsdl:service /wsdl:definitions