gpt4 book ai didi

java - Jersey : Unable to Get Sub-Sub-Resource

转载 作者:行者123 更新时间:2023-11-28 22:11:30 27 4
gpt4 key购买 nike

我有一个使用 Jersey 实现 REST API 的 Web 应用程序。 Web 容器 id Tomcat
以下是 API 的摘要:

/rest/patients
获取患者元数据列表。

/rest/patients/{id}
获取有关特定患者的详细数据。

/rest/patients/{id}/visits
获取特定患者的就诊元数据列表。

/rest/patients/{id}/visits/{visitId}
获取有关特定患者的特定就诊的详细数据。

我的问题是我无法获取子子资源。例如,当我请求 /rest/patients/1 时,患者 #1 的详细数据被正确接收。
但是当我请求 /rest/patients/1/visits 时,我得到 404 错误,并且流程甚至没有进入 getVisits() 方法。
看起来当收到针对特定患者 ID 的请求 (patients/{id}) 时,Jersey 将其从 PatientsMetadataResource 正确定向到 PatientsResource
但是,当请求访问子资源时(patients/{id}/visits),Jersey 不会将其定向到 PatientsResource

那么我怎样才能将一个子资源连同它的所有子子资源一起放到同一个类中呢?

PatientsMetadataResource代码(名字有点模糊,需要改一下):

@Path("/patients")
public class PatientsMetadataResource {

@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getPatients(@QueryParam("page") int pageIndex) {
//.... Loads, Builds and returns the patients' metadata list correctly
}

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/{uid:\\d+}")
public PatientResource getPatient(@PathParam("uid") int uid) {
return new PatientResource(uid);
}

}

PatientResource 的代码:

public class PatientResource {

private final int uid;

public PatientResource(int uid) {
this.uid = uid;
}

@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getPatient() {
//Returns the patient correctly
System.out.println("A Patient was asked");
Patient patient = PersistentDataProvider.loadPatientByUid(uid);
return Response.ok(patient).build();
}

@GET
@Path("/visits")
@Produces(MediaType.APPLICATION_JSON)
public VisitsResource getVisits(@PathParam("uid") int patientUid) {
//The flow doesn't even enter here. A 404 is being returned instead.
System.out.println("Visits were asked");
return new VisitsResource(patientUid);
}
}

web.xml 中 Jersey 部分的代码:

<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>
com.sun.jersey.spi.container.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>il.co.site_building.dvardy.resources</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>

最佳答案

子资源定位器不应该有 HTTP 方法注释

// @GET    <--- Remove this
// @Produces(MediaType.APPLICATION_JSON)
@Path("/{uid:\\d+}")
public PatientResource getPatient(@PathParam("uid") int uid) {
return new PatientResource(uid);
}

它们的主要目的只是将请求转发到子资源类,而不是 GET/POST 等任何东西。当 Jersey 看到该 HTTP 方法注释时,它不再被视为子资源定位器。

此外,您不需要传递 id。它将相应地通过

@Path("parent")
class ParentResource {
@Path("{id}")
public ChildResource getChild() {
return new ChildResource();
}
}

class ChildResource {
@GET
public Response get(@PathParam("id") long id) {}

@GET
@Path("something")
public Response something(@PathParam("id") long id) {}
}

这里 GET 'parent/1' 转到 ChildResource.get,传递路径参数,GET parent/1/something 转到 ChilsResource.something,传递路径参数

关于java - Jersey : Unable to Get Sub-Sub-Resource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40058535/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com