rest(Representational State Transfer):表現層狀態轉化,它是一種風格,用於資源定位,例如:http://ip:port/user/student/001
和資源操作:http的GET,POST,PUT,DELETE四種操作分別對應數據庫的select,update,insert,delete
資源(Resources):網絡上的一個具體信息。它可以是一段文本、一張圖片,總之就是一個具體的實在,你可以用一個URI(統一資源定位符)指向它,每種資源對應一個特定的
URI。要獲取這個資源,訪問它的URI就可以,因此URI就成了每一個資源的地址或獨一無二的識別符。
表現層(Representation):把"資源"具體呈現出來的形式,叫做它的"表現層",比如,文本可以用txt格式表現,也可以用HTML格式、XML格式、JSON格式表現,
在HTTP請求的頭信息中Accept和Content-Type這兩個字段,是對"表現層"的描述。
狀態轉化:互聯網通信協議HTTP協議,是一個無狀態協議。這意味着,所有的狀態都保存在服務器端。因此,如果客戶端想要操作服務器,必須通過某種手段,讓服務器端發生"狀
態轉化"(State Transfer)。而這種轉化是建立在表現層之上的,所以就是"表現層狀態轉化"。
總結:什么是RESTful架構?
(1)每一個URI代表一種資源;
(2)客戶端和服務器之間,傳遞這種資源的某種表現層;
(3)客戶端通過四個HTTP動詞,對服務器端資源進行操作,實現"表現層狀態轉化"。
下面是發布一個restFull風格的實例:
服務端:
實體:
package entity; import java.util.Date; import javax.xml.bind.annotation.XmlRootElement; /** * 實體 */ @XmlRootElement(name="student") public class Pojo { private long id; //溫度 private String detail; //日期 private Date date; //最高 private int temperature_max; //最低 private int temperature_min; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getDetail() { return detail; } public void setDetail(String detail) { this.detail = detail; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } public int getTemperature_max() { return temperature_max; } public void setTemperature_max(int temperature_max) { this.temperature_max = temperature_max; } public int getTemperature_min() { return temperature_min; } public void setTemperature_min(int temperature_min) { this.temperature_min = temperature_min; } }
一定要在實體類的前邊加上annotation ,這樣才能讓信息在POJO和XML之間轉換
SEI:
package service; import java.util.List; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import entity.Pojo; /** * 接口 */ @WebService @Path("/student") public interface WeatherInterface { //MediaType //查詢學生列表 @GET @Path("/queryList/{type}") @Consumes(MediaType.APPLICATION_XML) public List<Pojo> queryWeather(@PathParam("type")String cityName); //查詢學生 @GET @Path("/query/{id}") @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) public Pojo queryById(@PathParam("id")long id); }
每個方法之前,要用annotation聲明http請求的method類型,比如GET,DELETE,POST, PUT
@Path("/room/{id}")中的id是一個參數,應該在方法的參數列表中聲明: public Pojo queryById(@PathParam("id")long id)
實現類:
package service; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; import com.sun.org.glassfish.gmbal.ParameterNames; import entity.Pojo; /** * 實現類 */ public class Impl implements WeatherInterface { @Override public List<Pojo> queryWeather(String cityName) { List<Pojo> list = new ArrayList<Pojo>(); //日歷附件 Calendar calendar = Calendar.getInstance(); int day = calendar.get(Calendar.DATE); Pojo pojo1 = new Pojo(); pojo1.setId(01); pojo1.setDetail("晴1"); pojo1.setDate(new Date()); pojo1.setTemperature_max(5); pojo1.setTemperature_min(-6); Pojo pojo2 = new Pojo(); pojo2.setId(02); pojo2.setDetail("晴2"); calendar.set(Calendar.DATE, day+1); pojo2.setDate(calendar.getTime()); pojo2.setTemperature_max(5); pojo2.setTemperature_min(-6); Pojo pojo3 = new Pojo(); pojo3.setId(003); pojo3.setDetail("晴3"); calendar.set(Calendar.DATE, day+2); pojo3.setDate(calendar.getTime()); pojo3.setTemperature_max(5); pojo3.setTemperature_min(-6); list.add(pojo1); list.add(pojo2); list.add(pojo3); return list; } @Override public Pojo queryById(long id) { Pojo pojo1 = new Pojo(); pojo1.setId(id); pojo1.setDetail("張三"); pojo1.setDate(new Date()); pojo1.setTemperature_max(5); pojo1.setTemperature_min(-6); return pojo1; } }
spring配置文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:jaxrs="http://cxf.apache.org/jaxrs" 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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd" > <!-- service --> <bean id="WeatherInterface" class="service.Impl" /> <!-- 通過jaxrs:server方式來配置webservice --> <jaxrs:server address="/weather"> <jaxrs:serviceBeans> <ref bean="WeatherInterface" /> </jaxrs:serviceBeans> </jaxrs:server> </beans>
xml配置文件:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" 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"> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 加載spring容器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- cxf的servlet --> <servlet> <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>/ws/*</url-pattern> </servlet-mapping> </web-app>
把項目部署到tomcat,地址欄輸入:http://localhost:8989/cxf_rest_spring_service/ws/