환경:
idea 15.0.2
jersey 1.3
tomcat 7.0
maven 3.3.3
1.idea Maven을 기반으로 webapp을 빌드하고
을 건너뜁니다. 2. 프로젝트가 빌드된 후 pom.xml 파일을 필수 프로젝트 패키지에 추가합니다.
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.7</version> <scope>test</scope> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-core</artifactId> <version>1.3</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-server</artifactId> <version>1.3</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-client</artifactId> <version>1.3</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.14</version> </dependency> <dependency> <groupId>javax.ws.rs</groupId> <artifactId>jsr311-api</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>asm</groupId> <artifactId>asm</artifactId> <version>3.2</version> </dependency>
3. pojo 클래스를 만듭니다.
@XmlRootElement public class Student { private int id; private String name; private String dept; public int getId() { return id; } public Student() { } public Student(int id, String name, String dept) { super(); this.id = id; this.name = name; this.dept = dept; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDept() { return dept; } public void setDept(String dept) { this.dept = dept; } }
동시에 리소스 클래스 생성:
@Path("/students") public class RestWsDemo { private static Logger logger = Logger.getLogger(RestWsDemo.class); private static int index = 1; private static Map<Integer,Student> studentList = new HashMap<Integer, Student>(); public RestWsDemo() { if(studentList.size()==0) { studentList.put(index, new Student(index++, "Frank", "CS")); studentList.put(index, new Student(index++, "Jersey", "Math")); } } @GET @Path("{studentid}") @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) public Student getMetadata(@PathParam("studentid") int studentid) { if(studentList.containsKey(studentid)) return studentList.get(studentid); else return new Student(0, "Nil", "Nil"); } @GET @Path("list") @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) public List<Student> getAllStudents() { List<Student> students = new ArrayList<Student>(); students.addAll(studentList.values()); return students; } @POST @Path("add") @Produces("text/plain") public String addStudent(@FormParam("name") String name, @FormParam("dept") String dept) { studentList.put(index, new Student(index++, name, dept)); return String.valueOf(index-1); } @DELETE @Path("delete/{studentid}") @Produces("text/plain") public String removeStudent(@PathParam("studentid") int studentid) { logger.info("Receieving quest for deleting student: " + studentid); Student removed = studentList.remove(studentid); if(removed==null) return "failed!"; else return "true"; } @PUT @Path("put") @Produces("text/plain") public String putStudent(@QueryParam("studentid") int studentid, @QueryParam("name") String name, @QueryParam("dept") String dept ) { logger.info("Receieving quest for putting student: " + studentid); if(!studentList.containsKey(studentid)) return "failed!"; else studentList.put(studentid, new Student(studentid, name, dept)); return String.valueOf(studentid); } }
생성 후 프로젝트 구조는 아래와 같습니다.
4. 다음과 같이 web.xml을 구성합니다.
<servlet> <servlet-name>jerseyws</servlet-name> <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> <init-param> <param-name>com.sun.jersey.config.property.resourceConfigClass</param-name> <param-value>com.sun.jersey.api.core.PackagesResourceConfig</param-value> </init-param> <init-param> <param-name>com.sun.jersey.config.property.packages</param-name> <param-value>cz.service</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>jerseyws</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping>
com.sun.jersey.config.property.packages의 속성 값은 리소스가 있는 패키지의 경로입니다
5.maven 설치 건너뛰기
6 .Tomcat 액세스 경로 http://localhost:8081/rest/students/list를 시작하고 다음 결과를 확인하세요.
7. 그 외 자원 획득 방법은 직접 테스트해 보세요