0
votes

I'm using gradle and my project is building fine. When I use Jetty to deploy the war I don't see my object.

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 Files

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;
}
}

I'm using gradle build war java -jar jetty-runner-9.1.0.M0.jar --port 8081 build/libs/Sample.war

And don't know where to get the error from basically. I don't see the json object but I also not sure how to log with jax-rs. Thank you in advance.

1

1 Answers

2
votes

jetty-runner does not create a logs directory for server based logging.

If you do nothing to configure logging in your war, then all of the Jetty logs are presented on the console.

However, since you are using JAX-RS, it would be wise to configure whatever logging libraries that JAX-RS is using in your war to do whatever you need it to do (set logger levels, write to file, etc)