我尝试使用根资源(用户)和两个子资源(朋友列表和配置文件)设计一个简单的REST API。所以我写了这个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();
}
}
这里是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;
}
我知道,我不应该对网址进行硬编码,而是一步一步!
顺便说一下,这是我在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用户/ abcd / amici和GET用户/ abcd / profile
进行导航那么,如何制作呢?
答案 0 :(得分: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);
}
}