在jersey中主要弄清楚這麼幾個註釋:java
一、@Path ,定義路徑。git
二、@GET、@POST、@PUT等,定義提交請求的方法。web
三、@Consumes,定義接收請求的參數格式,是JSON、XML,仍是form表單。json
四、@Produces,定義輸出的格式,輸出XML、JSON等格式。app
五、@XmlRootElement、@XmlElement、@XmlAttribute定義根元素、元素、屬性,使用這些註解能夠自定義xml的輸出格式。maven
六、@FormParam、@QueryParam、@PathParam,獲取單個參數的三種方式:分別是從表單獲取參數、從url問號以後獲取參數、獲取路徑參數。還能夠直接將POJO做爲方法參數,根據接收的參數格式自動封裝bean,好比底部代碼中的formJson(Member member)方法。ui
七、@Context,從容器的上下文獲取HttpServletRequest、HttpServletResponse等對象。this
源碼參考url
maven 依賴:其中jersey-json爲json的輸出提供支持。
spa
<!-- 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指定資源類所在的包路徑,可指定多個(用";"隔開)。
訪問方式:http://localhost:8080/jerseyDemo/jersey/member/xml 。
<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; } }