eclipse java restful_eclipse + maven + com.sun.jersey 创建 restful api

maven 创建 jersey 项目

736cb1779311981f95cb582f51a1360f.png

如果没找到 jersey archetype, 下载 maven 的 archetype xml, 然后导入 archetypes

e3e05fcb0e65999bc7a945f6f1e6e00b.png

运行

右击 main.java -> Run As -> Java Application

7020286c5ee49c3ef7682f7427a038f8.png

如果 pom.xml 报错: Missing artifact com.sun.jersey:jersey-client:jar:${jersey-

version}, 则需要修改 jersey 版本号, 找到 pom.xml 中的

${jersey-version}

改成

1.19.1

运行成功,在 Console 栏位下会显示

f5c7a9cec32c829da09932a79b5d5732.png

890866dcb2893a71e389692c03d919ef.png

5887f923cb2274d0eb0d4c9e83b8ce8a.png

支持返回 json 数据对象

在 pom.xml 添加

com.owlike

genson

0.99

在 java 文件中就可直接返回对象

@GET

@Path("hello")

@Produces(MediaType.APPLICATION_JSON)

public UserInfo hello(){

UserInfo user = new UserInfo();

user._id = "id";

user._name = "haha";

return user;

}

public class UserInfo{

String _id;

String _name;

public String getId(){

return this._id;

}

public void setId(String id){

this._id= id;

}

public String getName(){

return this._name;

}

public void setName(String name){

this._name = name;

}

}

66ea00ea3c41f0ad99466ec870d861c8.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值