Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用Jersey分隔子资源表示_Java_Json_Rest_Resources_Jersey - Fatal编程技术网

Java 如何使用Jersey分隔子资源表示

Java 如何使用Jersey分隔子资源表示,java,json,rest,resources,jersey,Java,Json,Rest,Resources,Jersey,我正试图设计一个简单的RESTAPI,其中包含一个根资源(用户)和两个子资源(好友列表和配置文件)。所以我写了这个cose @Path("users/{username}") public class UtentiResource { private UtenteResource prova; @POST @Consumes(MediaType.APPLICATION_JSON) public Response doPost (@PathParam("username") String us

我正试图设计一个简单的RESTAPI,其中包含一个根资源(用户)和两个子资源(好友列表和配置文件)。所以我写了这个cose

@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();
}


}
下面是UtenerSource,它表示单个用户,并与子资源关联

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,但是,一步一个脚印

顺便说一下,这就是我在获取/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导航


那么,如何做到这一点呢?

下面是对使用子资源的一个很好的描述,它可能会帮助您解决您遇到的问题:

我还没有验证下面的代码,所以可能会有拼写错误,但从结构上看,这是可行的。首先定义UteneCollectionResource,其子资源仅为UteneCollectionResource

@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();
}

}
以下是资料来源:

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

}

下面是关于使用子资源的一个很好的描述,也许它将帮助您解决您遇到的问题: