使用eclipse创建restful webservice 工程Word文档下载推荐.docx

上传人:b****3 文档编号:15721268 上传时间:2022-11-15 格式:DOCX 页数:8 大小:178.64KB
下载 相关 举报
使用eclipse创建restful webservice 工程Word文档下载推荐.docx_第1页
第1页 / 共8页
使用eclipse创建restful webservice 工程Word文档下载推荐.docx_第2页
第2页 / 共8页
使用eclipse创建restful webservice 工程Word文档下载推荐.docx_第3页
第3页 / 共8页
使用eclipse创建restful webservice 工程Word文档下载推荐.docx_第4页
第4页 / 共8页
使用eclipse创建restful webservice 工程Word文档下载推荐.docx_第5页
第5页 / 共8页
点击查看更多>>
下载资源
资源描述

使用eclipse创建restful webservice 工程Word文档下载推荐.docx

《使用eclipse创建restful webservice 工程Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《使用eclipse创建restful webservice 工程Word文档下载推荐.docx(8页珍藏版)》请在冰豆网上搜索。

使用eclipse创建restful webservice 工程Word文档下载推荐.docx

servlet-name>

/servlet-name>

servlet-class>

com.sun.jersey.spi.container.servlet.ServletContainer<

/servlet-class>

load-on-startup>

1<

/load-on-startup>

/servlet>

servlet-mapping>

url-pattern>

/services/*<

/url-pattern>

/servlet-mapping>

在工程的classpath下会引用下列包:

这些包可以从 

下载(使用maven2)

首先创建一个简单的类Category

importjavax.xml.bind.annotation.XmlRootElement;

@XmlRootElement

publicclassCategory{

 

privateintid;

privateStringname;

publicCategory(){

super();

}

public 

Category(intid,Stringname){

this.id=id;

this.name=name;

publicintgetId(){

returnid;

publicvoidsetId(intid){

this.id=id;

publicStringgetName(){

returnname;

publicvoidsetName(Stringname){

this.name=name;

使用@XmlRootElement标注该类

点击

图标创建webservice,选择restblog工程,Framework选择REST(JAX-RS)

Javaclass:

CategoryService

URLpath:

资源路径填写category,这样所有与category相关的资源都使用http:

//yourpath/services/category访问

LIfecycle:

Pre-request(JAX-RSdefault)每次请求时生成一个服务类实例(这里指CategoryService),singleton:

单例,只存在一个实例

Consumes:

接受的contentType包括application/xml,application/json通俗来说就是接受提交数据的格式。

Produces:

产生的数据格式选项同样为applicatin/xml,application/json等。

注意这里的Consumes/Produces是指请求http:

//yourpath/services/category的数据格式。

点击Add按钮添加服务方法,界面如下:

这是一个构造服务方法的界面,下面有代码预览。

在添加参数的时候,选择ParamType选项的意义:

Context:

这是一个标识该参数为请求上下文。

可以直接获得request的参数。

QueryParam:

uri?

之后的参数

PathParam:

uri中的参数如:

/category/{id}中的id就是PathParam

FormParam:

使用post提交的参数。

CookieParam:

Cookie参数。

HeaderParam:

请求的头部信息。

也许你已经注意到没有session参数,是的,所谓的"

无状态stateless"

多少就体现在这里,web服务不保存请求相关的信息。

综合上面的用法,下面的例子涵盖了常用的服务方法的写法,之中的处理过程暂时用硬编码。

package 

com.dawnpro.restblog;

import 

java.util.ArrayList;

java.util.List;

javax.ws.rs.Consumes;

javax.ws.rs.DELETE;

javax.ws.rs.DefaultValue;

javax.ws.rs.FormParam;

javax.ws.rs.GET;

javax.ws.rs.POST;

javax.ws.rs.PUT;

javax.ws.rs.Path;

javax.ws.rs.PathParam;

javax.ws.rs.Produces;

javax.ws.rs.core.Context;

javax.ws.rs.core.UriInfo;

org.codehaus.jettison.json.JSONException;

org.codehaus.jettison.json.JSONObject;

@Path("

category"

public 

class 

CategoryService 

{

@GET

@Produces( 

{"

application/json"

"

application/xml"

})

/**

getCategories 

产生json,xml两种数据格式,具体那种格式取决于contentType

*/

List<

Category>

getCategories() 

result 

=new 

ArrayList<

();

result.add(new 

Category(1,"

第一个分类"

));

Category(2,"

第二个分类"

return 

result;

{id}"

Category 

getCategory(@PathParam("

id"

) 

int 

id) 

new 

Category(id,"

id为"

+id+"

的类别"

);

json/{id}"

"

JSONObject 

getCategoryJson(@PathParam("

id){//产生json

JSONObject();

try 

o.put("

 

id);

name"

的category"

catch 

(JSONException 

e) 

e.printStackTrace();

o;

@PUT

add"

@Produces("

text/html"

@Consumes( 

String 

addCategory(Category 

category) 

System.out.println("

处理添加类别逻辑,接受的数据为id:

+category.getId()+"

name:

+category.getName());

ok"

;

@POST

addbyname"

addCategory(@FormParam("

categoryname"

@DefaultValue("

[未命名]"

cateogryname) 

处理添加类别逻辑,接受的数据为name:

+cateogryname);

添加类别"

+cateogryname+"

成功"

updatecategory"

updateCategory(Category 

处理更新类别逻辑,接受的数据为id:

@DELETE

delete/{id}"

deleteCategory(@PathParam("

处理删除类别逻辑,接受的数据为id:

+id);

commonProcess"

pu

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 经管营销 > 经济市场

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1