1

我正在使用 Jersey 1.15、JDK 1.6、Tomcat 7、Maven 2.2.1 创建一个简单的 Restful Web 服务,它应该从我映射的 POJO 返回一个 JSON 字符串。

这是我的pojo:

package com.myservice.domain;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Person {
   private String firstName;
   private String lastName;

  // Getters & Setters
}

我的网络服务:

package com.myservice.resource;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.myservice.domain.Person;

@Path("/service")
public class MyService {

    @GET
    @Path("showPerson")
    @Produces(MediaType.APPLICATION_JSON)
    public Person getPerson() {
       Person person = new Person();
       person.setFirstName("John");
       person.setLastName("Doe");
       return person;
    }
}

这是我的 pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0    
         http://maven.apache.org/xsd/maven-4.0.0.xsd">

         <modelVersion>4.0.0</modelVersion>
         <groupId>com.myservice</groupId>
         <artifactId>myservice</artifactId>
         <version>0.0.1-SNAPSHOT</version>
         <packaging>war</packaging>
         <name>My Web Service</name>

         <dependencies>
             <dependency>
                 <groupId>junit</groupId>
                 <artifactId>junit</artifactId>
                 <version>4.8.2</version>
                 <scope>test</scope>
             </dependency>

             <dependency>
                <groupId>com.sun.jersey</groupId>
                <artifactId>jersey-servlet</artifactId>
                <version>1.15</version>
                <scope>runtime</scope>
             </dependency>

            <dependency>
               <groupId>com.sun.jersey</groupId>
               <artifactId>jersey-server</artifactId>
               <version>1.15</version>
            </dependency>

            <dependency>
                  <groupId>com.sun.jersey</groupId>
                  <artifactId>jersey-core</artifactId>
                  <version>1.15</version>
            </dependency>

            <dependency>
                 <groupId>com.sun.jersey</groupId>
                 <artifactId>jersey-json</artifactId>
                 <version>1.14</version>
            </dependency>

           <dependency>
                <groupId>com.sun.jersey.contribs</groupId>
                <artifactId>jersey-multipart</artifactId>
                <version>1.14</version>
           </dependency>

           <dependency>
               <groupId>org.codehaus.jackson</groupId>
               <artifactId>jackson-jaxrs</artifactId>
               <version>1.9.9</version>
           </dependency>
    </dependencies>

     <build>
          <finalName>myservice</finalName>
          <plugins>
                <plugin>
                 <artifactId>maven-compiler-plugin</artifactId>
                  <configuration>
                       <source>1.6</source>
                       <target>1.6</target>
                  </configuration>
                </plugin>
          </plugins>
     </build>
</project>

我的 web.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                             http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0"
         metadata-complete="true">
     <display-name>My Web Service</display-name>
     <servlet>
       <servlet-name>Jersey REST Service</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.myservice.resource</param-value>
         </init-param>
         <init-param>
            <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
            <param-value>true</param-value>
         </init-param>
         <load-on-startup>1</load-on-startup>
     </servlet>
     <servlet-mapping>
          <servlet-name>Jersey REST Service</servlet-name>
          <url-pattern>/rest/*</url-pattern>
      </servlet-mapping>
</web-app>

部署到 tomcat 后,当我尝试像这样调用 Web 服务时:

curl -X GET http://localhost:8080/myservice/rest/service/showPerson

我收到以下错误:

Nov 20, 2012 10:40:06 AM com.sun.jersey.spi.container.ContainerResponse write
SEVERE: A message body writer for Java class com.myservice.domain.Person, 
and Java type class com.myservice.domain.Person, 
and MIME media type application/json was not found

Nov 20, 2012 10:40:06 AM com.sun.jersey.spi.container.ContainerResponse write
SEVERE: A message body writer for Java class com.myservice.domain.Person,
and Java type class com.myservice.domain.Person, and MIME media type 
application/json was not found

Nov 20, 2012 10:40:06 AM com.sun.jersey.spi.container.ContainerResponse write 
SEVERE: The registered message body writers compatible with the 
MIME media type are: */* ->

     com.sun.jersey.core.impl.provider.entity.FormProvider
     com.sun.jersey.core.impl.provider.entity.StringProvider
     com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
     com.sun.jersey.core.impl.provider.entity.FileProvider
     com.sun.jersey.core.impl.provider.entity.InputStreamProvider
     com.sun.jersey.core.impl.provider.entity.DataSourceProvider
     com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General
     com.sun.jersey.core.impl.provider.entity.ReaderProvider
     com.sun.jersey.core.impl.provider.entity.DocumentProvider
     com.sun.jersey.core.impl.provider.entity.StreamingOutputProviderfg
     com.sun.jersey.core.impl.provider.entity.SourceProvider$SourceWriter
     com.sun.jersey.server.impl.template.ViewableMessageBodyWriter
     com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General
     com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General

    Nov 20, 2012 10:40:06 AM com.sun.jersey.spi.container.ContainerResponse 
    write

      SEVERE: The registered message body writers compatible with the 
      MIME media type are:
      */* ->

     com.sun.jersey.core.impl.provider.entity.FormProvider
     com.sun.jersey.core.impl.provider.entity.StringProvider
     com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
     com.sun.jersey.core.impl.provider.entity.FileProvider
     com.sun.jersey.core.impl.provider.entity.InputStreamProvider
     com.sun.jersey.core.impl.provider.entity.DataSourceProvider
     com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General
     com.sun.jersey.core.impl.provider.entity.ReaderProvider
     com.sun.jersey.core.impl.provider.entity.DocumentProvider
     com.sun.jersey.core.impl.provider.entity.StreamingOutputProvider
     com.sun.jersey.core.impl.provider.entity.SourceProvider$SourceWriter
     com.sun.jersey.server.impl.template.ViewableMessageBodyWriter
     com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General
     com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General

    SEVERE: Mapped exception to response: 500 (Internal Server Error)

    javax.ws.rs.WebApplicationException: 
    com.sun.jersey.api.MessageException:

    A message body writer for Java class
    com.myservice.domain.Person, 
    and Java type class com.myservice.domain.Person, 
    and MIME media type application/json was not found

    com.sun.jersey.api.MessageException: 

    A message body writer for Java class com.myservice.domain.Person, 
    and Java type class com.myservice.domain.Person, and MIME media type  
    application/json was not found

    HTTP Status 500 - Internal Server Error 
    The server encountered an internal error that prevented it from 
    fulfilling this request.

上网一搜,我尝试将大家建议的如下提议的解决方案插入到web.xml中:

<init-param>
   <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
   <param-value>true</param-value>
</init-param>

但它仍然给我同样的问题!但是,如果我使用 APPLICATION_XML,它确实会返回编组为 XML 的正确数据。

如果有人能指出我正确的方向,我将不胜感激。

非常感谢您花时间阅读本文。

4

2 回答 2

7

在您的依赖项中添加Genson库。它将自动启用 JSON-POJO 数据绑定。

于 2012-11-20T21:59:16.253 回答
1

将 jersey-json 添加到您的依赖项中。它将添加一个 JSON 序列化实现。

将此添加到pom.xml

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-json</artifactId>
    <version>1.8</version>
</dependency>
于 2013-05-22T16:25:57.023 回答