前言 之前写过一篇整合ssm+cxf不过是soap webservice,另一种webservice的实现是Restful风格的。 SOAP Webservice和Restful Webservice的使用,首先需要理解就是SOAP偏向于面向活动,有严格的规范和标准
前言
之前写过一篇整合ssm+cxf不过是soap webservice,另一种webservice的实现是Restful风格的。
SOAP Webservice和Restful Webservice的使用,首先需要理解就是SOAP偏向于面向活动,有严格的规范和标准,包括安全,事务等各个方面的内容,同时SOAP强调操作方法和操作对象的分离,有WSDL文件规范和XSD文件分别对其定义。而REST强调面向资源,只要我们要操作的对象可以抽象为资源即可以使用REST架构风格。
开发环境
Eclipse+jdk1.8+tomcat
所需jar包
ssm框架的jar包和
示例
web.xml
<!-- 配置cxf的核心控制器 --> <servlet> <servlet-name>CXFServlet</servlet-name> <servlet-class> org.apache.cxf.transport.servlet.CXFServlet </servlet-class> <load-on-startup>2</load-on-startup> </servlet> <!-- 所有来自/webservice/*的请求交给cxf处理 --> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/webservice/*</url-pattern> </servlet-mapping>
spring-dao.xml添加引用
<import resource="classpath:spring/apache-cxf.xml"/>
apacha-cxf.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd"> <context:component-scan base-package="com.XX.dao" /> <import resource="classpath*:META-INF/cxf/cxf.xml" /> <jaxrs:server id="restServiceContainer" address="/"> <jaxrs:providers> <ref bean="jacksonProvider" /> </jaxrs:providers> <jaxrs:extensionMappings> <entry key="json" value="application/json" /> <entry key="xml" value="application/xml" /> </jaxrs:extensionMappings> <jaxrs:serviceBeans> <ref bean="testUserService" /> </jaxrs:serviceBeans> <jaxrs:inInterceptors> <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean> </jaxrs:inInterceptors> <jaxrs:outInterceptors> <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean> </jaxrs:outInterceptors> </jaxrs:server> <bean id="jacksonObjectMapper" class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean"></bean> <bean id="jacksonProvider" class="com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider"> <property name="mapper" ref="jacksonObjectMapper" /> </bean> </beans>
entity.java 一定要加注解
import java.util.Date; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "baseUser") public class TBaseUser { private String userId; private String userName; private String identityCode; private String password; private String realName; private String nickName; private String imageUrl; private String userWalletAddress; private String candyWalletAddress; private String eMail; private String phoneNumber; private String exchangeUserId; private Date creatTime; private Date updateTime; private Boolean isDelete; public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId == null ? null : userId.trim(); } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName == null ? null : userName.trim(); } public String getIdentityCode() { return identityCode; } public void setIdentityCode(String identityCode) { this.identityCode = identityCode == null ? null : identityCode.trim(); } public String getPassword() { return password; } public void setPassword(String password) { this.password = password == null ? null : password.trim(); } public String getRealName() { return realName; } public void setRealName(String realName) { this.realName = realName == null ? null : realName.trim(); } public String getNickName() { return nickName; } public void setNickName(String nickName) { this.nickName = nickName == null ? null : nickName.trim(); } public String getImageUrl() { return imageUrl; } public void setImageUrl(String imageUrl) { this.imageUrl = imageUrl == null ? null : imageUrl.trim(); } public String getUserWalletAddress() { return userWalletAddress; } public void setUserWalletAddress(String userWalletAddress) { this.userWalletAddress = userWalletAddress == null ? null : userWalletAddress.trim(); } public String getCandyWalletAddress() { return candyWalletAddress; } public void setCandyWalletAddress(String candyWalletAddress) { this.candyWalletAddress = candyWalletAddress == null ? null : candyWalletAddress.trim(); } public String geteMail() { return eMail; } public void seteMail(String eMail) { this.eMail = eMail == null ? null : eMail.trim(); } public String getPhoneNumber() { return phoneNumber; } public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber == null ? null : phoneNumber.trim(); } public String getExchangeUserId() { return exchangeUserId; } public void setExchangeUserId(String exchangeUserId) { this.exchangeUserId = exchangeUserId == null ? null : exchangeUserId.trim(); } public Date getCreatTime() { return creatTime; } public void setCreatTime(Date creatTime) { this.creatTime = creatTime; } public Date getUpdateTime() { return updateTime; } public void setUpdateTime(Date updateTime) { this.updateTime = updateTime; } public Boolean getIsDelete() { return isDelete; } public void setIsDelete(Boolean isDelete) { this.isDelete = isDelete; } }
service
@Path("/user") public interface TestUserService { @GET @Path("/getUser") @Produces({ MediaType.APPLICATION_JSON,@GET @Path("/getUser") @Produces({ MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) public List<TBaseUser> getAllUsers();MediaType.APPLICATION_XML }) public List<TBaseUser> getAllUsers(); }
serviceimpl
@Named("testUserService") public class TestUserServiceimpl implements TestUserService{ @Autowired private TBaseUserMapper tBaseUserMapper; public List<TBaseUser> getAllUsers(){ TBaseUserExample example= new TBaseUserExample(); List<TBaseUser> list=tBaseUserMapper.selectByExample(example); return list; } }
访问结果
访问http://localhost:8080/项目名/webservice/user/getUser?_type=json,返回Json
{"baseUser":[{"candyWalletAddress":"0x0686F106aD2194a7b012b18EC390F496042187B4","nickName":"测试","password":"123qwe","phoneNumber":18611112222,"userId":1},{"candyWalletAddress":"0x0686F106aD2194a7b012b18EC390F496042187B4","nickName":"ceshi","password":"ceshi1","phoneNumber":18831688664,"userId":2,"userName":"ceshi"}]}
或者http://localhost:8080/项目名/webservice/user/getUser?_type=xml,返回XML
参考链接:
https://www.cnblogs.com/zhuyiqizhi/p/6213502.html
https://blog.csdn.net/iamlake/article/details/51217303