java jersey restful,Java Jersey RESTful 服务器端Hello World入门

pom.xml

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

4.0.0

com.what21.demo

jersey-helloworld

war

0.0.1-SNAPSHOT

jersey-helloworld Maven Webapp

http://maven.apache.org

1.19

junit

junit

4.4

test

com.sun.jersey

jersey-core

${jersey.version}

com.sun.jersey

jersey-server

${jersey.version}

com.sun.jersey

jersey-json

${jersey.version}

com.sun.jersey

jersey-bundle

${jersey.version}

jersey-helloworld

org.apache.maven.plugins

maven-compiler-plugin

3.1

1.8

1.8

web.xmlweb-app PUBLIC

"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

"http://java.sun.com/dtd/web-app_2_3.dtd" >

Archetype Created Web Application

JerseyServletContainer

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

com.sun.jersey.config.property.packages

com.what21.demo.jersey.rest

com.sun.jersey.api.json.POJOMappingFeature

true

1

JerseyServletContainer

/rest/*

User.javapackage com.what21.demo.jersey.domain;

public class User {

private int id;

private String name;

public User(int id, String name) {

this.id = id;

this.name = name;

}

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;

}

}

UserServiceRest.javapackage com.what21.demo.jersey.rest;

import java.util.ArrayList;

import java.util.List;

import javax.ws.rs.GET;

import javax.ws.rs.Path;

import javax.ws.rs.PathParam;

import javax.ws.rs.Produces;

import javax.ws.rs.core.MediaType;

import com.what21.demo.jersey.domain.User;

@Path("/user")

public class UserServiceRest {

@GET

@Path("{id}")

@Produces(MediaType.APPLICATION_JSON)

public User getUserById(@PathParam("id") int id) {

User user = new User(id, "张三");

return user;

}

@GET

@Path("list")

@Produces(MediaType.APPLICATION_JSON)

public List getUserList() {

List userList = new ArrayList();

userList.add(new User(1, "张三"));

userList.add(new User(2, "李四"));

userList.add(new User(2, "王五"));

return userList;

}

}

测试访问:

http://127.0.0.1:8080/jersey-helloworld/rest/user/1

http://127.0.0.1:8080/jersey-helloworld/rest/user/list

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值