Apache CXF = Celtix + XFire,开始叫 Apache CeltiXfire,后来更名为 Apache CXF 了,以下简称为 CXF。CXF 继承了 Celtix 和 XFire 两大开源项目的精华,提供了对 JAX-WS 全面的支持,并且提供了多种 Binding 、DataBinding、Transport 以及各种 Format 的支持,并且可以根据实际项目的需要,采用代码优先(Code First)或者 WSDL 优先(WSDL First)来轻松地实现 Web Services 的发布和使用。Apache CXF已经是一个正式的Apache顶级项目。
1.在web.xml添加cxf对应的servlet
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/data/ws/*</url-pattern>
</servlet-mapping>
当我们输入http://locahost:8080/test/date/ws/,我们就能看到web下的所有webservice。
2.定义WEBService接口及实现
package com.vcredit.jdev.obiz.account.controller;
import java.net.MalformedURLException;
import javax.jws.WebParam;
import javax.jws.WebService;
import com.vcredit.jdev.obiz.ws.entity.CommonContent;
import com.vcredit.jdev.obiz.ws.entity.LoginContent;
import com.vcredit.jdev.obiz.ws.entity.WSResponse;
/**
* @ClassName: AccountWebService
* @Description: TODO(account module WebServcie interface CXF ?wsdl)
* @author dk
* @date 2014年8月18日 下午1:36:17
*
*/
@WebService
public interface AccountWebService {
public WSResponse<LoginContent> login(@WebParam(name="loginName") String loginName,@WebParam(name="password")String password);
public WSResponse<CommonContent> addLoginAccount(@WebParam(name="username") String username,@WebParam(name="mobile") String mobile,@WebParam(name="password") String password,@WebParam(name="verifyCode") String verifyCode) throws Exception;
public WSResponse<CommonContent> updatePassword(@WebParam(name="username") String username,@WebParam(name="secureCode") Long secureCode,@WebParam(name="oldPassword") String oldPassword,@WebParam(name="newPassword") String newPassword);
public WSResponse<CommonContent> resetPassword(@WebParam(name="mobile") String mobile,@WebParam(name="newPassword") String newPassword,@WebParam(name="verifyCode") String verifyCode);
public WSResponse<CommonContent> checkCellPhone(@WebParam(name="mobile") String mobile);
public WSResponse<CommonContent> checkVerifyCode(@WebParam(name="mobile") String mobile,@WebParam(name="verifyCode") String verifyCode);
public WSResponse<CommonContent> sendVerifyCode(@WebParam(name="mobile") String mobile) throws MalformedURLException;
}
实现类:
package com.vcredit.jdev.obiz.account.controller;
import java.net.MalformedURLException;
import javax.jws.WebService;
import net.gplatform.sudoor.server.security.model.auth.SSAuth;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.vcredit.jdev.obiz.account.model.Login;
import com.vcredit.jdev.obiz.account.model.PasswordReset;
import com.vcredit.jdev.obiz.account.model.PhoneService;
import com.vcredit.jdev.obiz.account.model.Register;
import com.vcredit.jdev.obiz.account.model.entity.Account;
import com.vcredit.jdev.obiz.base.BaseConstants;
import com.vcredit.jdev.obiz.ws.entity.CommonContent;
import com.vcredit.jdev.obiz.ws.entity.LoginContent;
import com.vcredit.jdev.obiz.ws.entity.WSResponse;
@Component
@WebService
public class AccountWebServiceImpl implements AccountWebService {
private Logger logger = LoggerFactory.getLogger(AccountWebServiceImpl.class);
@Autowired
private SSAuth auth;
@Autowired
private Register register;
@Autowired
private Login login;
@Autowired
private PasswordReset passwordReset;
@Autowired
private PhoneService phoneService;
@Override
public WSResponse<LoginContent> login(String loginName, String password) {
if(StringUtils.isBlank(loginName)){
return WSResponse.errorResponse(new LoginContent(false, "-2",-1L,"UserKind",-1L));
}
if(StringUtils.isBlank(password)){
return WSResponse.errorResponse(new LoginContent(false, "-2",-1L,"UserKind",-1L));
}
Account account = register.getAccountByLoginName(loginName);
if(account != null){
auth.authenticate(account.getId().toString(), password);
}else{
return WSResponse.errorResponse(new LoginContent(false, "-2",-1L,"UserKind",-1L));
}
return WSResponse.successResponse(new LoginContent(true, "2", account.getId(), "UserKind", 1L));
}
}
。。。。。
@WebService
@WebParam(name="loginName") 参数的设置,明确参数。当不设置,会出现arg0,arg1的表示参数
3.在spring.xml添加Bean
<?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">
<jaxws:endpoint id="accountServiceEndPoint" implementor="#accountServiceImpl" address="/soap/account/accountService" />
<jaxws:endpoint id="loginService" implementor="#loginServiceImpl" address="/soap/account/loginService" />
<jaxws:endpoint id="newConsumerService" implementor="#newConsumerServiceImpl" address="/soap/account/newConsumerService" />
<jaxws:endpoint id="featureService" implementor="#featureServiceImpl" address="/soap/account/featureService" />
<jaxws:endpoint id="userService" implementor="#userServiceImpl" address="/soap/account/userService" />
<jaxws:endpoint id="accountWebService" implementor="#accountWebServiceImpl" address="/soap/account/accountWebService" />
<jaxws:client id="accountServiceClient" serviceClass="com.vcredit.jdev.obiz.example.controller.AccountService" address="http://localhost:8080/obiz/data/ws/soap/account/accountService" />
</beans>
4.pom.xml
<properties>
<cxf.version>3.0.0</cxf.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>${cxf.version}</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<includes>
<include>*.wsdl</include>
</includes>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<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>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-ws-security</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>${cxf.version}</version>
</dependency>