自己写的一个WebService-CXF
首先先加在jar包
pom
<!-- CXF -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-core</artifactId>
<version>3.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.2</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
Spring的jar包就不写了。。之前有写过
web.xml
<!-- CXF dispatching servlet -->
<servlet>
<description>CXFServlet</description>
<display-name>CXF Servlet</display-name>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<!-- 字符过滤器 -->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
WebService:
/*
* Creation : 5 Jan 2016
*/
package com.webservice.cxf;
import javax.validation.Valid;
import com.webservice.cxf.request.XinxcRequest;
import com.webservice.cxf.response.XinxcResponse;
public interface XinxcService {
XinxcResponse callService(@Valid XinxcRequest xinxcRequest);
}
实现类:
/*
* Creation : 5 Jan 2016
*/
package com.webservice.cxf.Impl;
import javax.jws.WebService;
import com.webservice.cxf.XinxcService;
import com.webservice.cxf.request.XinxcRequest;
import com.webservice.cxf.response.ExampleResp;
import com.webservice.cxf.response.XinxcResponse;
@WebService(targetNamespace = "http://xin.xiu.com/", serviceName = "XinxcService")
public class XinxcServiceImpl implements XinxcService {
@Override
public XinxcResponse callService(XinxcRequest xinxcRequest) {
XinxcResponse xinxcResponse = new XinxcResponse();
xinxcResponse.setResultCode("0000");
xinxcResponse.setResultMsg("success");
ExampleResp exampleResp = new ExampleResp();
exampleResp.setCode("1");
exampleResp.setName("one");
xinxcResponse.setExampleResp(exampleResp);
return xinxcResponse;
}
}
用到的request和response bean:
/*
* Creation : 5 Jan 2016
*/
package com.webservice.cxf.request;
import java.util.List;
import javax.validation.Valid;
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 = "XinxcRequest", propOrder = { "requestCode", "xinxcInputs" })
public class XinxcRequest {
@XmlElement(required = true)
private String requestCode;
@XmlElement(required = true)
@Valid
private List<XinxcInput> xinxcInputs;
public String getRequestCode() {
return requestCode;
}
public void setRequestCode(String requestCode) {
this.requestCode = requestCode;
}
public List<XinxcInput> getXinxcInputs() {
return xinxcInputs;
}
public void setXinxcInputs(List<XinxcInput> xinxcInputs) {
this.xinxcInputs = xinxcInputs;
}
}
/*
* Creation : 5 Jan 2016
*/
package com.webservice.cxf.request;
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 = "XinxcInput", propOrder = { "code", "name" })
public class XinxcInput {
@XmlElement(required = true)
private String code;
@XmlElement
private String name;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
/*
* Creation : 5 Jan 2016
*/
package com.webservice.cxf.response;
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 = "ExampleResponse", propOrder = { "resultCode", "resultMsg", "exampleResp" })
public class XinxcResponse {
@XmlElement
private String resultCode;
/** The result msg. */
@XmlElement
private String resultMsg;
/** The example resp. */
@XmlElement(required = true)
private ExampleResp exampleResp;
public String getResultCode() {
return resultCode;
}
public void setResultCode(String resultCode) {
this.resultCode = resultCode;
}
public String getResultMsg() {
return resultMsg;
}
public void setResultMsg(String resultMsg) {
this.resultMsg = resultMsg;
}
public ExampleResp getExampleResp() {
return exampleResp;
}
public void setExampleResp(ExampleResp exampleResp) {
this.exampleResp = exampleResp;
}
}
/*
* Creation : 28 Oct 2015
*/
package com.webservice.cxf.response;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
/**
* The Class ExampleResp.
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ExampleResp", propOrder = { "code", "name" })
public class ExampleResp {
/** The code. */
@XmlElement
private String code;
/** The name. */
@XmlElement
private String name;
/**
* Getter code.
*
* @return the code
*/
public String getCode() {
return code;
}
/**
* Setter code.
*
* @param code the code to set
*/
public void setCode(String code) {
this.code = code;
}
/**
* Getter name.
*
* @return the name
*/
public String getName() {
return name;
}
/**
* Setter name.
*
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
}
另外注意:
applicationContext:
<beans xmlns:jaxws="http://cxf.apache.org/jaxws"
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.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-servlet.xml" />
<!-- WebService -->
<bean id="xinxcServiceImpl" class="com.webservice.cxf.Impl.XinxcServiceImpl" />
<jaxws:endpoint id="xinxcServiceEndpoint" implementor="#xinxcServiceImpl" address="/XinxcService" />
好了,看一下效果: