Gradle Jax-RS Mysql Jetty项目无效

时间:2017-07-26 12:15:39

标签: java gradle jetty jax-rs

我正在使用gradle,我的项目正在建设中。当我使用Jetty部署战争时,我没有看到我的对象。

apply plugin: 'war'
apply plugin: 'eclipse'

repositories {
   mavenCentral()
}

dependencies {
    compile 'org.glassfish.jersey.media:jersey-media-json-jackson:2.16'
    compile 'org.glassfish.jersey.bundles:jaxrs-ri:2.16'
}

compileJava {
    sourceCompatibility = JavaVersion.VERSION_1_8
    targetCompatibility = JavaVersion.VERSION_1_8
}

Java文件

Persons.java

package workspace.TomcatJNDIProject;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Id;

public class Persons implements Serializable {

private static final long serialVersionUID = 1L;
@Basic(optional = false)
@Column(name="person_id")
@Id
private int personId;
@Basic(optional = false)
@Column(name="name")
@Id
private String name;
private int Age;
private Date DOB;
private String email;

@JsonProperty(value = "person_id")
public int getId() {
    return personId;
}
public void setId(int id) {
    this.personId = id;
}

@JsonProperty(value = "name")
public String getName() {
    return name;
}

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

@JsonProperty(value = "age")
public int getAge() {
    return Age;
}
public void setAge(int age) {
    Age = age;
}

@JsonProperty(value = "dateofbirth")
public Date getDOB() {
    return DOB;
}
public void setDOB(Date dOB) {
    DOB = dOB;
}

@JsonProperty(value = "email")
public String getEmail() {
    return email;
}
public void setEmail(String email) {
    this.email = email;
}
}

PeopleDAO.java

package workpace.TomcatJNDIProject;

import javax.ws.rs.core.Response;

import com.people.model.Persons;

public interface PeopleDAO {

public Response getPerson(int id);
public Response getAllPeople();

}

PeopleDAOImpl.java

 package workpace.TomcatJNDIExample;

import java.sql.Connection; 
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.apache.log4j.Logger;
import com.poeple.dao.PeopleDAO;
import com.poeple.model.Persons;
import com.poeple.model.StatusMessage;
import com.poeple.util.Database;

public class PeopleDAOImpl implements PeopleDAO {
private DataSource datasource = Database.getDataSource();
private Logger logger = Logger.getLogger(PeopleDAOImpl.class);

@Override
public Response getPerson(int id) {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;

    People person = null;
    String sql = "select people_id, first_name, last_name, address, city, "
         + "state, zip_code, is_active from Person where people_id = ?";

    try {
        conn = datasource.getConnection();
        ps = conn.prepareStatement(sql);
        ps.setInt(1, id);
        rs = ps.executeQuery();

        if (rs.next()) {
            person = new People();
            person.setId(rs.getInt("Person_ID"));
            person.setName(rs.getString("Name"));
            person.setAge(rs.getInt("Age"));
            person.setDOB(rs.getDate("DateOfBirth"));
            person.setEmail(rs.getString("Email"));
            persons.add(person);
        } else {
            logger.error(String.format("Person with ID of %d is not found.", id));
            StatusMessage statusMessage = new StatusMessage();
            statusMessage.setStatus(Status.NOT_FOUND.getStatusCode());
            statusMessage.setMessage(String.format("Customer with ID of %d is not found.", id));
            return Response.status(404).entity(statusMessage).build();
        }
    } catch (SQLException e) {
        logger.error("Error: " + e.getMessage());
        e.printStackTrace();
    }
    finally {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                logger.error("Error closing resultset: " + e.getMessage());
                e.printStackTrace();
            }
        }
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException e) {
                logger.error("Error closing PreparedStatement: " + e.getMessage());
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                logger.error("Error closing connection: " + e.getMessage());
                e.printStackTrace();
            }
        }
    }
    return Response.status(200).entity(people).build();
}
}

TomcatJNDIService.java

package workpace.TomcatJNDIProject;

import java.io.IOException; 
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType; 
import javax.ws.rs.core.Response;
import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import com.people.dao.PeopleDAO;
import com.people.dao.impl.PeopleDAOImpl;
import com.people.model.Persons;

@Path("tomcat")
public class TomcatJNDI {
private Logger logger = Logger.getLogger(TomcatJNDIExample.class);  


@GET
@Path("/people/{id}")
@Produces(MediaType.APPLICATION_JSON)
public Response getPerson(@DefaultValue("0") @QueryParam("id") int id) {

    PeopleDAO daoImpl = new PeopleDAOImpl();
    logger.info("Inside getPerson...");

    Response resp = daoImpl.getPerson(id);
    return resp;
}
}

StatusMessage.java

package com.avaldes.model;

import org.codehaus.jackson.annotate.JsonProperty;

public class StatusMessage {

private Integer status;
private String message;

public StatusMessage() {
}

@JsonProperty(value = "status_code")
public Integer getStatus() {
    return status;
}

public void setStatus(Integer status) {
    this.status = status;
}

@JsonProperty(value = "message")
public String getMessage() {
    return message;
}

public void setMessage(String message) {
    this.message = message;
}
}

我正在使用 gradle build war java -jar jetty-runner-9.1.0.M0.jar --port 8081 build / libs / Sample.war

并且不知道基本上从哪里得到错误。我没有看到json对象,但我也不确定如何使用jax-rs进行登录。提前谢谢。

1 个答案:

答案 0 :(得分:2)

jetty-runner不会为基于服务器的日志记录创建日志目录。

如果您没有在战争中配置日志记录,那么所有Jetty日志都会显示在控制台上。

但是,由于您使用的是JAX-RS,因此配置JAX-RS在战争中使用的任何日志库以执行您需要执行的操作(设置记录器级别,写入文件等)是明智的。