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。
-
j2ee与java的区别
JTS是CORBAOTS事务监控的基本实现规定了事务管理的实现方法.下面是小编整理的关于j2ee与java的区别,欢迎大家参考!1、Java分类Java分为JavaSE(Java标准版)、J2EE(Java企业版)和JavaME(Java微型版);JavaSE(JavaStandardEdition),一般用来开发桌面应用程序,但是在开...
-
J2EEWeb模拟考试题及答案
J2EE是一套全然不同于传统应用开发的技术架构,包含许多组件,主要可简化且规范应用系统的开发与部署,进而提高可移植性、安全与再用价值。那么J2EEWeb考试题你会做吗?下面跟yjbys小编一起来试试看咯!1、JSP的内置对象及方法。(1)request表示HttpServletRequest对象...
-
Java的技术点和知识点归纳
今天yjbys小编为大家介绍的主要内容是和J2SE相关的部分,另外,会在以后再介绍些J2EE相关的、和Java中各个框架相关的内容。经过这么多年的Java开发,以及结合平时面试Java开发者的一些经验,对于J2SE方面主要就是要掌握以下的一些内容。1.JVM相关(包括了各个版本的特...
-
J2EE学习步骤
1.当然Java语法得先看一看的,I/O包,Util包,Lang包你都熟悉了吗?2.然后再从JSP学起。如果你学过HTML,那么事情要好办的多,如果没有,那你快去补一补HTML基础吧。其实JSP中的Java语法也不多,它更象一个脚本语言,有点象ASP。3.然后你就该学一学Servlet了。Servlet就是服务...
相关文章
- XFire创建WebService实例
- 六级英语写作范文:The Celebration of Western Festivals
- Oracle认证:Freebufferwaits等待事件总结
- Efforts Urged to Improve Womens Reproductive Health英语阅读
- 英语作文:Water Is the Source of Life
- Myfavoritesubjectisscience教案
- Perseverance will make you successful英语作文
- 英文求职信:Customer Services Executive
- 英语优秀作文:M Unfrgettable Experiences
- 初一英语Unit 12 My favorite subject is science 课后练习题答