【问题标题】:How to separate subresource representations with Jersey如何使用 Jersey 分隔子资源表示
【发布时间】:2014-12-12 05:33:17
【问题描述】:

我正在尝试设计一个简单的 REST API,其中包含一个根资源(用户)和两个子资源(朋友列表和个人资料)。所以我写了这个角色

@Path("users/{username}")
public class UtentiResource
{

private UtenteResource prova;

@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response doPost (@PathParam("username") String username , AuthDTO auth)
{
    new UtenteService ().registrazione(username, auth.getPassword(), auth.getEmail());


    return Response.ok().build();

}

@GET
@Produces(MediaType.APPLICATION_JSON)
public UtenteResource doGet (@PathParam("username") String username)
{

    prova = new UtenteResource (username);
    return prova;



}

@Path("amici")
public AmiciResource getAmici ()
{
    return prova.getAmiciRes();
}


@Path ("/profile")
public ProfiloResource getProfilo()
{
    return prova.getProfiloRes();
}


}

这里是UtenteResource,它代表单个用户并与子资源相关联

public class UtenteResource
{


private String username;
private String profilo;
private String amici;

private ProfiloResource profiloRes;
private AmiciResource amiciRes;

public UtenteResource (String username)
{
    this.username = username;
    this.profilo = URIs.UTENTE_RES + "/" + username + URIs.PROFILO_SUBRES;
    this.amici = URIs.UTENTE_RES + "/" + username + URIs.AMICI_SUBRES;

}

public String getProfilo()
{
    return profilo;
}

public String getAmici()
{
    return amici;
}



public String getUsername()
{
    return username;
}

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

@GET
@Produces(MediaType.APPLICATION_JSON)
public ProfiloResource getProfiloRes()
{
    return profiloRes;
}

@GET
@Produces(MediaType.APPLICATION_JSON)
public AmiciResource getAmiciRes()
{
    return amiciRes;
}

我知道,我不应该对 URL 进行硬编码,而是一步一步来!

顺便说一句,这是我在执行 GET /users/abcd 时得到的

{

"username": "abcd",
"profilo": "http://localhost:8084/nice2mit_backend/restAPI/users/abcd/profile",
"amici": "http://localhost:8084/nice2mit_backend/restAPI/users/abcd/amici",
"profiloRes": null,
"amiciRes": null

}

但我不想在我的响应正文中出现“profiloRes”和“amiciRes”,因为我想让他们使用 GET users/abcd/amici 和 GET users/abcd/profile 进行导航

那么,如何制作呢?

【问题讨论】:

标签: java json rest resources jersey


【解决方案1】:

这里是使用子资源的一个很好的描述,也许它会帮助你找出你遇到的问题:

How is the PUT request in this example using subresource is processed by JAX-RS run time?

我尚未验证以下代码,因此可能存在拼写错误,但从结构上讲,这是可行的。首先,您定义 UtenteCollectionResource,其子资源仅为 UtenteResource

@Path("users")
public class UtenteCollectionResource
{

/*
 * Path annotation with param username defined here when using sub-resources,
 * not in the top-level resource
 */
@Path({username})
public UtenteResource doGet (@PathParam("username") String username)
{
    return new UtenteResource (username);
}

/*
 * Provide @GET, @PUT, @POST, @DELETE to get collection of containers
 *
 * Please note that the following is not best-practice -- you should POST to the
 * collection when creating a new element, not POST to specific element.  The
 * following may work for you, but please revisit after you get sub-resources
 * working.  The username should NOT be coming in on the path in this case.
 */
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Path({username})
public Response doPost (@PathParam("username") String username , AuthDTO auth)
{
    new UtenteService ().registrazione(username, auth.getPassword(), auth.getEmail());
    return Response.ok().build();
}

}

这里是 UtenteResource:

@Produces(MediaType.APPLICATION_JSON)
public class UtenteResource
{

private String username;
private String profiloPath;
private String amiciPath;

/*
 * Constructor allowing it to be used as sub-resource
 */
public UtenteResource (String username)
{
    this.username = username;
    this.profiloPath = URIs.UTENTE_RES + "/" + username + URIs.PROFILO_SUBRES;
    this.amiciPath = URIs.UTENTE_RES + "/" + username + URIs.AMICI_SUBRES;
}

/*
 * Provide @GET, @PUT, @POST, @DELETE to get specific utente
 * Notice that path params are not redefined...
 */
@GET
public Utente getUtente() {
    return new Utente(username, profilioPath, amiciPath);
}    

/*
 * Define sub-resource for profilio
 */
@Path("profilio")
public ProfiloResource getProfiloResource()
{
    return new ProfilioResource(username);
}

/*
 * Define sub-resource for amici
 */
@Path("amici")
public AmiciResource getAmiciResource()
{
    return new AmiciResource(username);
}

/*
 *  These getters/setters will have nothing to do with the REST exposure
 */
public String getProfiloPath() { return profiloPath; }
public String getAmiciPath() { return amiciPath; }
public String getUsername() { return username; }
public void setUsername(String username) { this.username = username; }

}

我不确定 amici 和 profilio 有效负载中返回了哪些内容,所以这里只是这些子资源的概述:

@Produces(MediaType.APPLICATION_JSON)
public class AmiciResource {

private String username;

/*
 * Constructor allowing it to be used as sub-resource
 */
public AmiciResource (String username)
{
    this.username = username;
}

/*
 * Provide @GET, @PUT, @POST, @DELETE to get specific amici
 * Notice that path params are not redefined...
 */
@GET
public Amici getAmici() {
    // however you build the amici object here
    return new Amici(username);
}    

}

@Produces(MediaType.APPLICATION_JSON)
public class ProfilioResource {

private String username;

/*
 * Constructor allowing it to be used as sub-resource
 */
public ProfilioResource (String username)
{
    this.username = username;
}

/*
 * Provide @GET, @PUT, @POST, @DELETE to get specific profilio
 * Notice that path params are not redefined...
 */
@GET
public Profilio getProfilio() {
    // however you build the profilio object here
    return new Profilio(username);
}    

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-19
    • 2014-02-20
    • 1970-01-01
    • 1970-01-01
    • 2011-02-14
    • 1970-01-01
    • 2012-11-30
    • 2015-07-28
    相关资源
    最近更新 更多