从对象提供JSON的Java Web服务

时间:2015-03-06 20:17:16

标签: java json jersey

我正在使用Eclipse Luna,Tomcat 7和Jersey尝试创建一个简单的Web服务。除了将对象作为JSON提供之外,一切正常。我可以通过手工编写字符串或使用JSONSimple来使其工作。但是我想依靠这个项目的更高级别的自动JSON转换。

我一整天都在阅读文档和Google搜索。但我无法过去:

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 test.model.User, and Java type class test.model.User, and MIME media type application/json was not found.

所有示例都让它看起来应该是自动的。

有人可以指出我缺少的东西吗?

由于

lib包括:(不使用Maven)

asm-3.3.1.jar c3p0-0.9.1.1.jar commons-codec-1.6.jar commons-collections4-4.0.jar commons-io-2.4.jar commons-lang3-3.3.jar commons-math3-3.2.jar javax.json-api-1.0.jar javax.servlet.jsp.jstl-1.2.1.jar javax.servlet.jsp.jstl-api-1.2.1.jar jersey-bundle-1.19.jar log4j-1.2.16.jar quartz-2.2.1.jar quartz-jobs-2.2.1.jar slf4j-api-1.6.6.jar slf4j-log4j12-1.6.6.jar taglibs-string.jar

否则我有:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>test1</display-name>
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<session-config>
    <session-timeout>180</session-timeout>
    <tracking-mode>COOKIE</tracking-mode>
</session-config>
<listener>
    <listener-class>test.util.AppInitListener</listener-class>
</listener>
<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.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>

package test.rest;

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

import test.dao.UserDao;
import test.model.User;

@Path("/users")
public class RestUsers {

    private UserDao userDao;

    public RestUsers(){
        userDao = new UserDao();
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public User getUsers() {

        User user = userDao.getUserById(1L);

        return user;

    }

}

package test.model;

import java.io.Serializable;
import javax.persistence.*;
import java.util.Date;


/**
 * The persistent class for the users database table.
 * 
 */
@Entity
@Table(name="users")
@NamedQuery(name="User.findAll", query="SELECT u FROM User u")
public class User implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    private Long id;

    private String answer;

    @Temporal(TemporalType.TIMESTAMP)
    private Date created;

    private String email;

    private String handle;

    private String image;

    @Temporal(TemporalType.TIMESTAMP)
    private Date lastlogin;

    @Temporal(TemporalType.TIMESTAMP)
    private Date modified;

    private String name;

    private String passencr;

    private String passhint;

    private String password;

    private String question;

    private String username;

    public User() {
    }

    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getAnswer() {
        return this.answer;
    }

    public void setAnswer(String answer) {
        this.answer = answer;
    }

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

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

    public String getEmail() {
        return this.email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getHandle() {
        return this.handle;
    }

    public void setHandle(String handle) {
        this.handle = handle;
    }

    public String getImage() {
        return this.image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public Date getLastlogin() {
        return this.lastlogin;
    }

    public void setLastlogin(Date lastlogin) {
        this.lastlogin = lastlogin;
    }

    public Date getModified() {
        return this.modified;
    }

    public void setModified(Date modified) {
        this.modified = modified;
    }

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

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

    public String getPassencr() {
        return this.passencr;
    }

    public void setPassencr(String passencr) {
        this.passencr = passencr;
    }

    public String getPasshint() {
        return this.passhint;
    }

    public void setPasshint(String passhint) {
        this.passhint = passhint;
    }

    public String getPassword() {
        return this.password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getQuestion() {
        return this.question;
    }

    public void setQuestion(String question) {
        this.question = question;
    }

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

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

}

...也

Starting test1: class test.util.AppInitListener
log4j:WARN No appenders could be found for logger (org.quartz.impl.StdSchedulerFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Mar 06, 2015 6:01:04 PM com.sun.jersey.api.core.servlet.WebAppResourceConfig init
INFO: Scanning for root resource and provider classes in the Web app resource paths:
  /WEB-INF/lib
  /WEB-INF/classes
Mar 06, 2015 6:01:05 PM com.sun.jersey.api.core.ScanningResourceConfig logClasses
INFO: Root resource classes found:
  class test.rest.Hello
  class test.rest.RestUser
  class test.rest.RestUsers
  class test.rest.ListConfigs
Mar 06, 2015 6:01:05 PM com.sun.jersey.api.core.ScanningResourceConfig init
INFO: No provider classes found.
Mar 06, 2015 6:01:05 PM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFO: Initiating Jersey application, version 'Jersey: 1.19 02/11/2015 05:39 AM'
Mar 06, 2015 6:01:06 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Mar 06, 2015 6:01:06 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Mar 06, 2015 6:01:06 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 3307 ms

0 个答案:

没有答案