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。
相關文章
-
六級英語寫作範文:The Celebration of Western Festivals
Directions: For this part you are allowed 30 minutes to write a short essay entitled The Celebration of Weste Festivals. You should write at least 150 words following the outline given below.1. 現在國 -
Perseverance will make you successful英語作文
As is known to all: success lies in perseverance. nothing can make people much more excitation than success. but you must suffer all kinds of hardships throughout your life before you succeed. everyon -
英語優秀作文:M Unfrgettable Experiences
以下是求職網小編輯收集整理的,提供一篇交警入黨轉正申請書例文,為您提供幫助!敬愛的黨組織:你好!xx年x月x日,經支部大會討論通過,上級黨組織批准,我成為了一名光榮的中國xx預備黨員,至今已近一年。根據《黨章》第一章第七條之 -
Oracle認證:Freebufferwaits等待事件總結
總結就是對一個時期的學習、工作或其完成情況進行一次全面系統的回顧和分析的書面材料,他能夠提升我們的書面表達能力,因此我們需要回頭歸納,寫一份總結了。總結一般是怎麼寫的呢?下面是小編精心整理的食堂人員個人總結,希 -
Myfavoritesubjectisscience教案
以下是為您推薦的Myfavoritesubjectisscience教案,希望本篇文章對您學習有所幫助。SectionATeachingdesign:Topic:FavoritesubjectsFunction:Preferencesandgivereasons.Vocabulary:subject,science,biology,P.E.,beca -
七年級英語Unit 12 My favorite subject is science 課後練習題答
Unit 12 My favorite subject is scienceSeetion AIa 1.C 2.d 3.b 4.e 5.a 6.Flb circled items: science, art, math, music.2a The sentences are numbered left to right in this order:4 2 1 33a (1)your (2)subject (3)My (4 -
Efforts Urged to Improve Womens Reproductive Health英語閱讀
Efforts Urged to Improve Women's Reproductive HealthParliamentarians from over 100 countries are being urged to take action in improving women's reproductive health ahead of the 2015 deadline -
英語作文:Water Is the Source of Life
As the source of life, water is indispensable (不可缺少的) to human beings. People consume much water in production and daily life. Agriculture and industry account for most of mankind's water usag -
英文求職信:Customer Services Executive
Application for the position of Customer Service ExecutiveI am very interested in the captioned position that appeared in the January 4 of the JobsPower。com。 Enclosed please find my resume for your -
XFire建立WebService例項
鄉鎮是安全生產監管體系中最基層的一級,也是安全生產工作中最關鍵的一級,鄉鎮直接面對的是群眾,是落實安全生產法律法規的最終執行者。今天本站小編給大家帶來了20xx年鄉鎮安全生產工作總結,希望對大家有所幫助。20xx