我的REST API没有返回任何内容

时间:2014-07-21 04:30:06

标签: java xml rest entitymanager

我有一个实体类Workplaces.java:

@Entity
@Table(name = "workplaces")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Workplaces.findAll", query = "SELECT w FROM Workplaces w"),
    @NamedQuery(name = "Workplaces.findBySpId", query = "SELECT w FROM Workplaces w WHERE w.spId = :spId"),
    @NamedQuery(name = "Workplaces.findByWorkers", query = "SELECT w FROM Workplaces w WHERE w.workers = :workers"),
    @NamedQuery(name = "Workplaces.findByLatitude", query = "SELECT w FROM Workplaces w WHERE w.latitude = :latitude"),
    @NamedQuery(name = "Workplaces.findByLongitude", query = "SELECT w FROM Workplaces w WHERE w.longitude = :longitude")})
public class Workplaces implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    @Column(name = "sp_id")
    private Integer spId;
    @Basic(optional = false)
    @NotNull
    @Column(name = "workers")
    private int workers;
    @Basic(optional = false)
    @NotNull
    @Column(name = "latitude")
    private float latitude;
    @Basic(optional = false)
    @NotNull
    @Column(name = "longitude")
    private float longitude;

    public Workplaces() {
    }

    public Workplaces(Integer spId) {
        this.spId = spId;
    }

    public Workplaces(Integer spId, int workers, float latitude, float longitude) {
        this.spId = spId;
        this.workers = workers;
        this.latitude = latitude;
        this.longitude = longitude;
    }

    public Integer getSpId() {
        return spId;
    }

    public void setSpId(Integer spId) {
        this.spId = spId;
    }

    public int getWorkers() {
        return workers;
    }

    public void setWorkers(int workers) {
        this.workers = workers;
    }

    public float getLatitude() {
        return latitude;
    }

    public void setLatitude(float latitude) {
        this.latitude = latitude;
    }

    public float getLongitude() {
        return longitude;
    }

    public void setLongitude(float longitude) {
        this.longitude = longitude;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (spId != null ? spId.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Workplaces)) {
            return false;
        }
        Workplaces other = (Workplaces) object;
        if ((this.spId == null && other.spId != null) || (this.spId != null && !this.spId.equals(other.spId))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.rest.api.Workplaces[ spId=" + spId + " ]";
    }

}

我的WorkplacesFacadeREST.java的片段

@Stateless
@Path("com.rest.api.workplaces")
public class WorkplacesFacadeREST extends AbstractFacade<Workplaces> {
    @PersistenceContext(unitName = "com.rest_hiring_challenge_war_1.0-SNAPSHOTPU")
    private EntityManager em;

    public WorkplacesFacadeREST() {
        super(Workplaces.class);
    }

@GET
@Path("{id}")
@Produces({"application/xml", "application/json"})
public Workplaces find(@PathParam("id") Integer id) {
    return super.find(id);
}

@GET
@Override
@Produces({"application/xml", "application/json"})
public List<Workplaces> findAll() {
    return super.findAll();
}

我尝试了请求http://localhost:8080/hiring_challenge/com.rest.api.workplaces/500015631。我知道我的mysql db的500015631表中存在主键id workplaces。我也尝试过:http://localhost:8080/hiring_challenge/com.rest.api.workplaces/。结果页面只显示:

This XML file does not appear to have any style information associated with it. The document tree is shown below.

,没有别的。

更新 我错过了MySQL的jdbc驱动程序。添加之后,该项目工作正常。

1 个答案:

答案 0 :(得分:0)

您的JAX-RS服务是否未在web.xml或javax.ws.rs.core.Application的子类中注册?

一种方法是在web.xml中设置应用程序路径:

<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_3_0.xsd" version="3.0">
<servlet>
    <servlet-name>javax.ws.rs.core.Application</servlet-name>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>javax.ws.rs.core.Application</servlet-name>
    <url-pattern>/rest/api/*</url-pattern>
</servlet-mapping>

另一种选择是使用@ApplicationPath注释类打包您的应用程序,该类扩展了javax.ws.rs.core.Application:

@ApplicationPath("rest/api")
public class RESTConfig extends javax.ws.rs.core.Application{ }

路径“rest / api”只是一个示例,您也可以将其设置为root,但是为了部署JAX-RS服务,需要其中一个配置。