Jackson 的RESTEasy JSON示例

RESTEasy是JBOSS提供的用于构建 RESTful Web服务和RESTful Java应用程序的JAX-RS规范的实现。尽管这不仅限于仅在JBOSS中使用,您还可以与其他服务器一起使用。

Jackson是一个用于处理JSON数据格式的多功能Java库。 Jackson 的目标是为开发人员提供快速,正确,轻巧和人体工程学的最佳组合。

在这篇文章中,我将演示如何将Jackson与RESTEasy一起使用,以JSON格式转换API响应。

使用环境:

  1. 可重用2.3.1.GA
  2. RESTEasy Jackson提供商2.3.1
  3. 雄猫7
  4. JDK 1.6

请按照以下步骤构建演示应用程序。

1)创建一个Maven项目并转换为Eclipse Web项目

mvn原型:generate -DgroupId = com.how2codex -DartifactId = RESTfulDemoApplication 
-DarchetypeArtifactId = maven-archetype-webapp -DinteractiveMode = false

mvn eclipse:eclipse -Dwtpversion = 2.0

2)更新pom.xml中的运行时依赖项

  <modelVersion>4.0.0</modelVersion>
  <groupId>com.how2codex</groupId>
  <artifactId>RESTfulDemoApplication</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>RESTfulDemoApplication Maven Webapp</name>
  <repositories>
    <repository>
      <id>jboss</id>
      <url>http://repository.jboss.org/maven2</url>
    </repository>
  </repositories>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <!-- core library -->
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
         <artifactId>resteasy-jaxrs</artifactId>
        <version>2.3.1.GA</version>
    </dependency>
    <dependency>
        <groupId>net.sf.scannotation</groupId>
        <artifactId>scannotation</artifactId>
        <version>1.0.2</version>
    </dependency>
    <!-- Jackson support -->
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jackson-provider</artifactId>
        <version>2.3.1.GA</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>RESTfulDemoApplication</finalName>
  </build>
</project>

3)更新web.xml文件以实现Resteasy特定的servlet映射

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
<web-app>
    <display-name>Archetype Created Web Application</display-name>
    <!-- Auto scan REST service -->
    <context-param>
        <param-name>resteasy.scan</param-name>
        <param-value>true</param-value>
    </context-param>
    
    <servlet>
        <servlet-name>resteasy-servlet</servlet-name>
        <servlet-class>
            org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
        </servlet-class>
    </servlet>
 
    <servlet-mapping>
        <servlet-name>resteasy-servlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

4)编写服务类和模型类

User.java

package com.how2codex.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;
    }
}

UserManagementModule.java

package com.how2codex.service;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
import com.how2codex.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();
    }
}

5)运行应用程序

当我们在tomcat中部署以上构建的应用程序并单击URL:“ http:// localhost:8080 / RESTfulDemoApplication / user-management / users / 10”时,以下是响应。

 

RESTEasy + Jackson的例子
RESTEasy + Jackson的例子

 

单击下面的代码下载此示例的源代码。

 

下载源代码

 

学习愉快!

saigon has written 1440 articles

Leave a Reply