j2ee培訓:如何構建RESTful Web Service
JavaEE的核心是EJB3.0, 其提供了更兼便捷的企業級的應用框架。下面yjbys小編為大家準備了關於如何構建RESTful Web Service的.文章,歡迎閲讀。
1. 首先是實體類,注意其中的@XmlRootElement註解
package s;
import ;
import ootElement;
@XmlRootElement(name="Customer")
public class Customer {
private String id;
private String name;
private Date birthday;
public String getId() {
return id;
}
public void setId(String id) {
= id;
}
public String getName() {
return name;
}
public void setName(String name) {
= name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
hday = birthday;
}
@Override
public String toString() {
return ectionToString(this);
}
}
2. RESTful Web Service接口類,可以通過修改@Produces註解來聲明暴露接口返回的json還是xml數據格式
package s;
import ;
import ;
import Param;
import uces;
import yParam;
@Path(value = "/customer")
@Produces("*/*")
//@Produces("application/xml")
//@Produces("application/json")
public interface CustomerService {
@GET
@Path(value = "/{id}/info")
Customer findCustomerById(@PathParam("id")String id);
@GET
@Path(value = "/search")
Customer findCustomerByName(@QueryParam("name")String name);
}
3. RESTful Web Service接口實現類
package s;
import ndar;
public class CustomerServiceImpl implements CustomerService {
public Customer findCustomerById(String id) {
Customer customer = new Customer();
d(id);
ame(id);
irthday(nstance()ime());
return customer;
}
public Customer findCustomerByName(String name) {
Customer customer = new Customer();
d(name);
ame(name);
irthday(nstance()ime());
return customer;
}
}
4. Server端代碼
package s;
import ingInInterceptor;
import ingOutInterceptor;
import SServerFactoryBean;
public class MyServer {
public static void main(String[] args) throws Exception {
JAXRSServerFactoryBean factoryBean = new JAXRSServerFactoryBean();
nInterceptors()(new LoggingInInterceptor());
utInterceptors()(new LoggingOutInterceptor());
esourceClasses(s);
ddress("http://localhost:9000/ws/jaxrs");
te();
}
}
5. Client端代碼
package s;
import Client;
import Status;
import ethod;
public class MyClient {
public static void main(String[] args) throws Exception {
go("http://localhost:9000/ws/jaxrs/customer/1/info");
go("http://localhost:9000/ws/jaxrs/customer/search?name=abc");
}
private static void go(String url) throws Exception {
HttpClient client = new HttpClient();
GetMethod method = new GetMethod(url);
int statusCode = uteMethod(method);
if (statusCode != _OK) {
tln("Method failed: " + tatusLine());
}
byte[] responseBody = esponseBody();
tln(new String(responseBody));
}
}
6.測試
首先運行MyServer類,然後運行MyClient類來驗證Web Service。
-
2016年Jsp面試題及答案
JSP全名為JavaServerPages,中文名叫java服務器頁面,其根本是一個簡化的Servlet設計,它是由SunMicrosystems公司倡導、許多公司參與一起建立的一種動態網頁技術標準。下面YJBYS小編為大家整理了關於Jsp面試題及答案,希望對你有所幫助。1、jsp有哪些內置對象?作用分...
-
J2EE應用程序中的六種範圍類型
J2EE應用程序中有哪些範圍類型?J2EE應用程序的範圍類型是什麼?下面隨yjbys小編一起來學習吧!大多數服務器端J2EE應用程序中有六種常用的範圍類型:Transaction(事務)Request(請求)HTTPsession(HTTP會話)Application(應用程序)Global(全局)None(無)事務範圍事務範...
-
2016年J2EE認證測試題及答案
J2EE:電子商務和信息技術的快速發展以及對它們的需求給應用程序開發人員帶來了新的壓力。必須以比以前更少的金錢、更少的資源來更快地設計、開發企業應用程序。那麼關於J2EE認證考試你通過了嗎?下面yjbys小編為大家分享的是J2EE認證測試題及答案,希望對大家有...
-
J2EE簡介
J2EE是一套全然不同於傳統應用開發的技術架構,包含許多組件,主要可簡化且規範應用系統的開發與部署,進而提高可移植性、安全與再用價值。J2EE核心是一組技術規範與指南,其中所包含的各類組件、服務架構及技術層次,均有共同的標準及規格,讓各種依循J2EE架構的不同平台...
相關文章
- 七年級英語Unit 12 My favorite subject is science 課後練習題答
- Perseverance will make you successful英語作文
- Efforts Urged to Improve Womens Reproductive Health英語閲讀
- XFire創建WebService實例
- Myfavoritesubjectisscience教案
- 英語優秀作文:M Unfrgettable Experiences
- Oracle認證:Freebufferwaits等待事件總結
- 六級英語寫作範文:The Celebration of Western Festivals
- 英語作文:Water Is the Source of Life
- 英文求職信:Customer Services Executive