j2ee培训:如何构建RESTful Web Service
JavaEE的核心是EJB3.0, 其提供了更兼便捷的企业级的应用框架。下面本站小编为大家准备了关于如何构建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。
-
2017计算机应用基础题库附带答案
NIT合格证书由教育部考试中心签发,是证书持有人计算机应用能力的证明。本站小编收集了一些关于计算机应用基础题库附带答案,希望大家认真阅读!【1】计算机应用基础题库附带答案1、[单选题]在计算机中,一条指令代码由操作码和__C_____两部分组成。(A)、指令码(B)...
-
计算机二级office高级应用试题及答案
一、单项选择题1.将程序像数据一样存放在计算机内存中运行,是1946年由提出的。A)图灵B)布尔C)冯·诺依曼D)爱因斯坦2.世界上生产微处理(CPU)芯片的厂家很多,就是其中之一。A)IBM公司B)微软公司C)Intel公司D)联想公司3.计算机能按人们的意图自动地进行操作...
-
厦门大学2017年3月计算机等级考试报名通知
厦门大学2017年3月计算机等级考试报名统一实行网上报名,不接纳现场报名。2017年考试继续使用2013版考试体系,同时对部分级别和科目进行调整,二级Access数据库程序设计(科目代码29)将使用新版考试大纲(2016年版),详细如下:一、关键时间:1、网上报名时间厦门大学思明...
-
2017年计算机二级mos office考试试题及答案「精选」
计算机应用是计算机学科与其他学科相结合的边缘学科,是计算机学科的组成部分。下面是小编整理的关于计算机二级mosoffice考试试题及答案,希望大家认真阅读!1[单选题]下列叙述中正确的是().A.结点中具有两个指针域的链表一定是二叉链表B.结点中具有两个指针域的链...