1.spring web基础环境
见之前章节创建的web工程testRest。
2.REST地址
与上一节类似,设计RESTEasy模块/restCxf/*。
3.RESTEasy库
在项目pom.xml中加入RESTEasy依赖:
3.1.0 ... org.apache.cxf cxf-rt-frontend-jaxws ${restCxf.version} org.apache.cxf cxf-rt-transports-http ${restCxf.version} org.apache.cxf cxf-rt-transports-http-jetty ${restCxf.version} org.apache.cxf cxf-rt-frontend-jaxrs ${restCxf.version} ... org.apache.cxf cxf-rt-rs-client ${restCxf.version}
加入cxf jaxrs库、http库和client库。
4.Servlet
配置RESTEasy访问入口。
restCxf org.apache.cxf.transport.servlet.CXFServlet 1 restCxf /restCxf/*
restCxfServlet将拦截/restCxf/*下的所有访问。
5.实现api
新建com.sunbin.test.restCxf包,新增TeachersResource类,以实现/teachers路径的接口:
package com.sunbin.test.restCxf; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.ws.rs.FormParam; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.sunbin.test.teacher.pojo.Teacher; import com.sunbin.test.teacher.service.TeacherService; @Component @Path("teachers") @Produces(MediaType.APPLICATION_JSON) public class TeachersResource { @Autowired private TeacherService teacherService; @GET public Map get(@Context HttpServletRequest arg0) { System.out.println("RestCxf TeachersResource.get:" + arg0.getSession().getId()); Map map = new HashMap(); map.put("teachers", teacherService.list()); return map; } @POST public Map post(@FormParam("age") int age,@FormParam("name") String name){ Map map = new HashMap(); try { Teacher teacher = new Teacher(); teacher.setName(name); teacher.setAge(age); System.out.println("RestCxf TeachersResource.post:"+teacher); teacherService.save(teacher); map.put("status", "y"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return map; } }
路径注解@Path、返回类型注解@Produces、访问方法@GET、参数读取@FormParam,都和Jersey的JAX-RS相同。
新增TeacherResource类,以实现/teacher/{id}路径的接口:
package com.sunbin.test.restCxf; import java.util.HashMap; import java.util.Map; import javax.ws.rs.DELETE; import javax.ws.rs.FormParam; import javax.ws.rs.GET; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.sunbin.test.teacher.pojo.Teacher; import com.sunbin.test.teacher.service.TeacherService; @Component @Path("teacher/{id}") @Produces(MediaType.APPLICATION_JSON) public class TeacherResource { @Autowired private TeacherService teacherService; @GET public Map get(@PathParam("id") int id) { System.out.println("RestCxf TeacherResource.get:"+id); Teacher teacher = new Teacher(); teacher.setId(id); Map map = new HashMap(); map.put("teacher", teacherService.get(teacher)); return map; } @PUT public Map put(@PathParam("id") int id,@FormParam("age") int age,@FormParam("name") String name){ Map map = new HashMap(); try { Teacher teacher = new Teacher(); teacher.setId(id); teacher.setName(name); teacher.setAge(age); System.out.println("RestCxf TeacherResource.put:"+id+":"+teacher); teacherService.update(teacher); map.put("status", "y"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return map; } @DELETE public Map delete(@PathParam("id") int id){ Map map = new HashMap(); try { System.out.println("RestCxf TeacherResource.delete:"+id); Teacher teacher = new Teacher(); teacher.setId(id); teacherService.remove(teacher); map.put("status", "y"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return map; } }
6.js测试
使用上一节的测试页面src\main\webapp\rest\index.jsp进行测试:
...