RESTEasy使用json返回的例子
创建一个json的model类:
- package com.howtodoinjava.model;
- import java.io.Serializable;
- import javax.xml.bind.annotation.XmlAccessType;
- import javax.xml.bind.annotation.XmlAccessorType;
- import javax.xml.bind.annotation.XmlAttribute;
- import javax.xml.bind.annotation.XmlElement;
- import javax.xml.bind.annotation.XmlRootElement;
- @XmlAccessorType(XmlAccessType.NONE)
- @XmlRootElement(name = "user")
- public class User implements Serializable {
- private static final long serialVersionUID = 1L;
- @XmlAttribute(name = "id")
- private int id;
- @XmlElement(name = "firstName")
- private String firstName;
- @XmlElement(name = "lastName")
- private String lastName;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getFirstName() {
- return firstName;
- }
- public void setFirstName(String firstName) {
- this.firstName = firstName;
- }
- public String getLastName() {
- return lastName;
- }
- public void setLastName(String lastName) {
- this.lastName = lastName;
- }
- }
2、创建一个服务器回调类:
- UserManagementModule.java
- package com.howtodoinjava.service;
- import javax.ws.rs.GET;
- import javax.ws.rs.Path;
- import javax.ws.rs.PathParam;
- import javax.ws.rs.core.Response;
- import com.howtodoinjava.model.User;
- @Path("/user-management")
- public class UserManagementModule
- {
- @GET
- @Path("/users/{id}")
- @Produces("application/json")
- public Response getUserById(@PathParam("id") Integer id)
- {
- User user = new User();
- user.setId(id);
- user.setFirstName("Lokesh");
- user.setLastName("Gupta");
- return Response.status(200).entity(user).build();
- }
- }
3、应用测试
posted on 2016-08-12 09:38 nickTimer 阅读(1751) 评论(0) 编辑 收藏 举报