Java Jersey中的JSON Rest Webservice

时间:2014-08-20 06:29:05

标签: java json web-services rest jersey

我似乎无法弄清楚为什么我在尝试从Jersey REST webservice返回JSON时会遇到此异常。

我和Jersey,No Maven有一个Web动态项目。

Aug 20, 2014 2:15:16 AM com.sun.jersey.spi.container.ContainerResponse logException
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.examples.rest.StoredSearch, and Java type class   com.examples.rest.StoredSearch, and MIME media type application/json was not found


    //web.xml

   <servlet>
    <servlet-name>jersey-Employee-serlvet</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.examples.rest</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-Employee-serlvet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

</web-app>

//在WEB-INF / lib下 ASM-3.1jar
新泽西州的核心1.16.jar
新泽西州JSON-1.16.jar
新泽西服务器1.16.jar
Jersey的servlet的1.16.jar

我还添加了jersey-json-1.16.jar来构建路径。

//@XmlRootElement
public class StoredSearch {
String username;
String name;
String description;
Date created;

public String getUsername() {
    return username;
}
public void setUsername(String username) {
    this.username=username;
}

public String getName() {
    return name;
}
public void setName(String name) {
    this.name=name;
}

public String getDescription() {
    return description;
}
public void setDescription(String description) {
    this.description=description;
}

public Date getCreated() {
    return created;
}
public void setCreated(Date created) {
    this.created = created;
}

@Override
public String toString() {
    return "StoredSearch [username=" + username + ", name=" + name + ", description=" +   description + ", created=" + created + "]";
}


@GET
@Path("/search/{a}")
@Produces(MediaType.APPLICATION_JSON)
public StoredSearch getStoredSearch(@PathParam("a") String user){

    StoredSearch ss = new StoredSearch();
    ss.setUsername("ssss");
    ss.setName("yoo");
    ss.setDescription("description of something");
    ss.setCreated(new Date());

    //return ss.toString();
    return ss;

2 个答案:

答案 0 :(得分:0)

在项目中添加jaxb-api-XXX.jar

并在你的pojo中使用@XmlRootElement

JAX-RS支持从JAXB带注释的类到XML和JSON的自动映射。

它对我有用。

答案 1 :(得分:0)

我也遇到了类似的问题,这个问题是由于JARS丢失所以尝试了这些JARS,我解决了问题,我认为这将有效。

JARS列表:

 1. asm-3.1.jar
 2. jackson-core-asl-1.9.2.jar
 3. jackson-mapper-asl-1.9.2.jar
 4. jackson-jaxrs-1.9.2.jar    
 5. jackson-xc-1.9.2.jar.
 6. jersey-client-1.17.1.jar.
 7. jersey-core-1.17.1.jar.
 8. jersey-json-1.17.1.jar.
 9. jersey-server-1.17.1.jar
10.jersey-servlet-1.17.1.jar
11.jettison-1.1.jar 
12.jsr311-api-1.1.1.jar