CXF+Spring+JAXB+Json構建Restful服務
話不多說,先看具體的例子:
檔案目錄結構:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>platform</display-name>
<!-- Spring ApplicationContext配置檔案的路徑,可使用萬用字元,多個路徑用,號分隔 此引數用於後面的Spring Context
Loader -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/applicationContext.xml,classpath:/applicationContext-rs.xml
</param-value>
</context-param>
<!-- Character Encoding filter -->
<filter>
<filter-name>encodingFilter</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>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring 重新整理Introspector防止記憶體洩露 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<!-- Apache CXF Servlet 配置 -->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
Spring的xml—applicationContext.xml
<?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:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd"
default-lazy-init="true">
<description>Spring公共配置檔案</description>
<!-- 使用annotation 自動註冊bean,並保證@Required,@Autowired的屬性被注入 -->
<context:component-scan base-package="com.zd.daoImpl.*,com.zd.beanImpl.*,com.zd.serviceImpl.*,com.zd.service.*">
</context:component-scan>
<bean id="dozerMapper" class="org.dozer.DozerBeanMapper"
lazy-init="false" />
<!-- Matrix BPM -->
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED,-Exception</prop>
</props>
</property>
</bean>
</beans>
RestFul的xml—applicationContext-rs.xml<?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:jaxrs="http://cxf.apache.org/jaxrs"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<description>Rails CM 的所有RESTFUL 服務配置</description>
<context:component-scan base-package="com.zd" />
<bean id="jsonProvider" class="org.codehaus.jackson.jaxrs.JacksonJsonProvider"></bean>
<jaxrs:server id="restContainer" address="/">
<jaxrs:inInterceptors>
</jaxrs:inInterceptors>
<jaxrs:serviceBeans>
<ref bean="userinfoServiceImpl" />
</jaxrs:serviceBeans>
</jaxrs:server>
</beans>
實體:
package com.zd.entity;
import java.io.Serializable;
/** 系統使用者表 **/
public class UserInfo implements Serializable {
private String id;
private String name;// 使用者姓名
private String sex;// 性別
private String idNo;//身份證號碼
private String account;// 賬號
private String pwd;// 密碼
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getIdNo() {
return idNo;
}
public void setIdNo(String idNo) {
this.idNo = idNo;
}
}
構建的XML實體:package com.zd.dtd;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlRootElement(name="UserInfoDTO")
@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlType
public class UserInfoDTD {
private String id;
private String name;// 使用者姓名
private String sex;// 性別
private String idNo;//身份證號碼
private String account;// 賬號
private String pwd;// 密碼
@XmlElement(name="id",required=false)
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@XmlElement(name="name",required=false)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlElement(name="account",required=false)
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
@XmlElement(name="sex",required=false)
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@XmlElement(name="sex",required=false)
public String getIdNo() {
return idNo;
}
public void setIdNo(String idNo) {
this.idNo = idNo;
}
@XmlElement(name="sex",required=false)
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
介面服務層:
package com.zd.service;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@Path("/Userinfo")
public interface UserinfoService {
/**
* 根據id獲取使用者資訊
* @param id 使用者id
* @return json資料
*/
@GET
@Path("/getUserinfo")
@Produces({ "text/html;charset=utf-8", MediaType.TEXT_HTML })
public Response getBasicUserInfo(@QueryParam("id") String id);
}
服務實現層:
package com.zd.serviceImpl;
import javax.ws.rs.core.Response;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.zd.beanImpl.UserBeanImpl;
import com.zd.dtd.UserInfoDTD;
import com.zd.entity.UserInfo;
import com.zd.service.UserinfoService;
@Component
public class UserinfoServiceImpl implements UserinfoService {
@Autowired(required=true)
private UserBeanImpl userBeanImpl;
@Override
public Response getBasicUserInfo(String id) {
UserInfo userInfo = userBeanImpl.getUserInfo(id);
UserInfoDTD userDTD=new UserInfoDTD();
userDTD.setSex(userInfo.getSex());
userDTD.setId(userInfo.getId());
userDTD.setIdNo(userInfo.getIdNo());
userDTD.setName(userInfo.getName());
userDTD.setPwd(userInfo.getPwd());
userDTD.setAccount(userInfo.getAccount());
//json轉換
JSONArray jsonArray = JSONArray.fromObject(userInfo);
return Response.ok(jsonArray.toString()).build();
}
}
ServiceBean:
package com.zd.beanImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.zd.daoImpl.UserinfoDaoImpl;
import com.zd.entity.UserInfo;
@Service
//預設將類中的所有函式納入事務管理.
@Transactional
public class UserBeanImpl {
@Autowired(required=true)
private UserinfoDaoImpl userinfoDaoImpl;
public UserInfo getUserInfo(String id){
UserInfo userInfo=userinfoDaoImpl.getUserinfo(id);
return userInfo;
}
}
Dao:
package com.zd.daoImpl;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.zd.entity.UserInfo;
@Repository
//預設將類中的所有函式納入事務管理.
@Transactional
public class UserinfoDaoImpl {
public UserInfo getUserinfo(String id){
UserInfo userInfo=new UserInfo();
userInfo.setAccount("zhudan");
userInfo.setId("1111");
userInfo.setId("520911188119186770");
userInfo.setName("朱丹");
userInfo.setPwd("1221");
userInfo.setSex("女");
return userInfo;
}
}
頁面訪問地址:
http://localhost:8080/restful/rest/Userinfo/getUserinfo
頁面訪問結果:
總結:
先由這個例子,巨集觀瞭解下Restful的服務搭建,Restful的細節東西再在以後的運用中深入理解
相關文章
- spring boot構建restful服務Spring BootREST
- 使用 Jersey 和 Apache Tomcat 構建 RESTful Web 服務ApacheTomcatRESTWeb
- 教你 10 分鐘構建一套 RESTful API 服務 ( 上 )RESTAPI
- 使用 Spring 3 MVC HttpMessageConverter 功能構建 RESTful web 服務SpringMVCHTTPRESTWeb
- 搭建 Restful Web 服務RESTWeb
- 構建Web API服務WebAPI
- 使RESTful Web服務更加實用的10個建議RESTWeb
- 構建應用層服務
- JEESZ架構、分散式服務:Dubbo+Zookeeper+Proxy+Restful架構分散式REST
- 構建WCF RESTful service示例REST
- 車聯網服務non-RESTful架構改造實踐REST架構
- 用 Golang 構建 gRPC 服務GolangRPC
- swoole 服務的建構函式函式
- 負載均衡-構建CDN服務負載
- 用 Hystrix 構建高可用服務架構架構
- 使用CXF開發RESTFul服務REST
- RESTFul Web Api 服務框架(一)RESTWebAPI框架
- SpringCloud構建微服務架構-Hystrix服務降級SpringGCCloud微服務架構
- hyperf從零開始構建微服務(一)——構建服務提供者微服務
- hyperf從零開始構建微服務(二)——構建服務消費者微服務
- GreatSQL 構建高效 HTAP 服務架構指南(MGR)SQL架構
- Spring Cloud構建微服務架構—服務容錯保護(Hystrix服務降級)SpringCloud微服務架構
- 用 GIN 構建一個 WEB 服務Web
- Nginx網站服務與LNMP構建Nginx網站LNMP
- 構建SpringCloud閘道器服務SpringGCCloud
- Spring Cloud構建微服務架構-服務閘道器SpringCloud微服務架構
- Spring Cloud構建微服務架構-Hystrix服務降級SpringCloud微服務架構
- yii2 restful web服務路由RESTWeb路由
- Yii2.0 RESTful Web服務(3)RESTWeb
- Yii2.0 RESTful Web服務(4)RESTWeb
- 如何使用RestTemplate訪問restful服務REST
- 第19章 建立RESTful Web服務RESTWeb
- 使用SpringBoot構建REST服務-什麼是REST服務Spring BootREST
- 使用Golang和MongoDB構建 RESTful APIGolangMongoDBRESTAPI
- 使用Swashbuckle構建RESTful風格文件REST
- 構建基於RocketMQ的分散式事務服務MQ分散式
- Spring Cloud構建微服務架構服務消費RibbonSpringCloud微服務架構
- Spring Cloud構建微服務架構—服務消費(Feign)SpringCloud微服務架構