标签:
在jersey中主要弄清楚这么几个注释:
1、@Path ,定义路径。
2、@GET、@POST、@PUT等,定义提交请求的方法。
3、@Consumes,定义接收请求的参数格式,是JSON、XML,还是form表单。
4、@Produces,定义输出的格式,输出XML、JSON等格式。
5、@XmlRootElement、@XmlElement、@XmlAttribute定义根元素、元素、属性,使用这些注解可以自定义xml的输出格式。
6、@FormParam、@QueryParam、@PathParam,获取单个参数的三种方式:分别是从表单获取参数、从url问号之后获取参数、获取路径参数。还可以直接将POJO作为方法参数,根据接收的参数格式自动封装bean,比如底部代码中的formJson(Member member)方法。
7、@Context,从容器的上下文获取HttpServletRequest、HttpServletResponse等对象。
maven 依赖:其中jersey-json为json的输出提供支持。
<!-- jersey-servlet: 包含了jersey-servlet/jersey-core/jersey-server --> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-servlet</artifactId> <version>1.19</version> </dependency> <!-- jersey-json: 包含了jaxb-impl/jackson-core-asl/jackson-mapper-asl等 --> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-json</artifactId> <version>1.19</version> </dependency>
web.xml中的配置,com.sun.jersey.config.property.packages指定资源类所在的包路径,可指定多个(用";"隔开)。
<servlet> <servlet-name>jersey</servlet-name> <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> <init-param> <param-name>com.sun.jersey.config.property.packages</param-name> <param-value>com.jersey.demo.resources</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>jersey</servlet-name> <url-pattern>/jersey/*</url-pattern> </servlet-mapping>
MemberResource:
package com.jersey.demo.resources; import javax.servlet.http.HttpServletRequest; import javax.ws.rs.Consumes; import javax.ws.rs.FormParam; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.jersey.demo.core.entity.Member; /** * jersey demo * * @author sean * */ @Path("/member") public class MemberResource { private Logger logger = LoggerFactory.getLogger(getClass()); private Member createMember() { Member member = new Member(); member.setEmail("657620636@qq.com"); member.setName("张三"); member.setStreet("南山区白石洲天河街55号"); member.setZip("518025"); return member; } /** * 输出xml * * @return */ @Path("/xml") @GET @Produces(value = { MediaType.APPLICATION_XML }) public Member xml() { logger.debug("jersey xml"); Member member = createMember(); return member; } /** * 输出json。不知为何,不加上"; charset=UTF-8",输出的中文就是乱码 * * @return */ @Path("/json") @GET @Produces(value = { MediaType.APPLICATION_JSON + "; charset=UTF-8" }) public Member json() { logger.debug("jersey json"); Member member = createMember(); return member; } /** * 接收表单请求,输出xml * * @param name * @return */ @Path("/formParam") @POST @Consumes(value = { MediaType.APPLICATION_FORM_URLENCODED }) @Produces(value = { MediaType.APPLICATION_XML }) public Member formParam(@FormParam("name") String name) { logger.debug("formParam"); Member member = createMember(); member.setName(name); return member; } /** * 根据传送过来的json自动封装表单参数到member的属性;输出xml * * @param member * @return */ @Path("/formJson") @POST @Consumes(value = { MediaType.APPLICATION_JSON }) @Produces(value = { MediaType.APPLICATION_XML }) public Member formJson(Member member) { logger.debug("formJson"); return member; } /** * 获取url携带的参数,即"?"之后的参数;输出xml * * @param name * @return */ @Path("/queryParam") @GET @Produces(value = { MediaType.APPLICATION_XML }) public Member queryParam(@QueryParam("name") String name) { logger.debug("queryParam"); Member member = createMember(); member.setName(name); return member; } /** * 获取路径参数,输出xml * * @param name * @return */ @Path("/pathParam/{name}") @GET @Produces(value = { MediaType.APPLICATION_XML }) public Member pathParam(@PathParam("name") String name) { logger.debug("pathParam"); Member member = createMember(); member.setName(name); return member; } /** * 从上下文获取HttpServletRequest;输出xml * * @param request * @return */ @Path("/request") @GET @Produces(value = { MediaType.APPLICATION_XML }) public Member request(@Context HttpServletRequest request) { logger.debug("request"); String name = request.getParameter("name"); Member member = createMember(); member.setName(name); return member; } }
Member:
package com.jersey.demo.core.entity; import java.io.Serializable; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class Member implements Serializable { /** * */ private static final long serialVersionUID = 1L; private Long id; private String name; private String zip; private String street; private String email; public Long getId() { return id; } public void setId(Long id) { this.id = id; } /** * 定义xml属性 * * @return */ @XmlAttribute public String getName() { return name; } public void setName(String name) { this.name = name; } /** * 定义为xml元素。默认为@XmlElement * * @return */ @XmlElement public String getZip() { return zip; } public void setZip(String zip) { this.zip = zip; } public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
标签:
原文地址:http://my.oschina.net/u/2007041/blog/425058