j2ee培训:如何构建RESTful Web Service
来源:才华咖 本文已影响7.43K人
来源:才华咖 本文已影响7.43K人
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。
Myfavoritesubjectisscience教案
高中生优秀英语作文:Have a break away from electronics
初一英语Unit 12 My favorite subject is science 课后练习题答
英语作文:Water Is the Source of Life
XFire创建WebService实例
英语阅读:Selective Courses
TOEFL词汇:representative
英语阅读:Life is inevitable consequence of physics
Efforts Urged to Improve Womens Reproductive Health英语阅读
雅思阅读模拟试题:Sun's fickle heart may leave us cold
英文求职信:Customer Services Executive
A Letter to the University President为题目的作文
英语短文:Celebration of Chinese New Year
新起点英语一上Review three Lesson的教案
TV Receivers in Students Flats大学英语作文
英语作文:An unforgettable experience
大学生面临许多压力The Pressures on Collese Students英语作文
优秀英语作文:Double Seventh Festival
英语作文Never judge a book by its cover
It was nice to see your letter为题目的作文
刷牙的英语作文:If You Never Brushed Your Teeth
Box Jellyfish Eyes Aim At The Trees美文欣赏
Five Sentences to Hurt Children英语作文
Unforgettable experience 难忘的一件事作文
英语阅读:African Union Leaders Talk Mali, Chinese Investmen
英语作文Let the Surprise Speak of Your Love
Covering the ears to steal the bell高中英语作文
英语范文:Stress-relieving Laughter
Java如何调用WebService
如何选职业规划培训和机构
Jolie drops battle over perfume named Shilohy英语作文
英文自荐信:Customer Services Supervisor
《如何说课》培训稿
英语阅读:Covergirl unveils empowering brand update
j2ee培训:如何构建RESTful Web Service
英语作文:大学生村官 College Graduates Work as Village Offic
UN Chief Urges Leaders to Deliver on Climate Change美文欣赏
如何搭建演讲稿的结构
MS SQL Server Management Studio Express安装图文具体步骤
大学英语演讲稿:Television News Coverage