1、创建Rest Web Service项目
点击完成,生成的web.xml内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<?xml version=
"1.0"
encoding=
"UTF-8"
?>
<web-app version=
"3.0"
xmlns=
"http://java.sun.com/xml/ns/javaee"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http:
//java.sun.com/xml/ns/javaee
http:
//java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<servlet>
<display-name>JAX-RS REST Servlet</display-name>
<servlet-name>JAX-RS REST Servlet</servlet-name>
<servlet-
class
>
com.sun.jersey.spi.container.servlet.ServletContainer
</servlet-
class
>
<load-on-startup>
1
</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JAX-RS REST Servlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
|
备注:可以添加REST Web Service capabilities,在项目上点击右键,选择MyEclipse>Add REST Web Service Capabilities。
2、创建普通Java类Customer.java
在类Customer名称前面添加@XmlRootElement,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
package
com.xju.ws;
import
javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public
class
Customer {
private
int
id;
private
String name;
private
String address;
public
int
getId() {
return
id;
}
public
void
setId(
int
id) {
this
.id = id;
}
public
String getName() {
return
name;
}
public
void
setName(String name) {
this
.name = name;
}
public
String getAddress() {
return
address;
}
public
void
setAddress(String address) {
this
.address = address;
}
}
|
2、创建CustomersResource类
添加第一个方法:
添加第二个方法:
添加第三个方法:
结果:
本文转自stock0991 51CTO博客,原文链接:http://blog.51cto.com/qing0991/1390768,如需转载请自行联系原作者