Java 如何调用依赖的rest服务
我正在使用rest easy构建rest服务。有两种资源-学生和课程以及学生和课程之间的1:N关系。 要获取学生详细信息,Url将为/students/1 获取课程详情:/students/1/课程/科学 源代码:Java 如何调用依赖的rest服务,java,rest,jax-rs,resteasy,Java,Rest,Jax Rs,Resteasy,我正在使用rest easy构建rest服务。有两种资源-学生和课程以及学生和课程之间的1:N关系。 要获取学生详细信息,Url将为/students/1 获取课程详情:/students/1/课程/科学 源代码: public class Student { private int id; private String name; private List<Course> courses; } public class Course { pr
public class Student {
private int id;
private String name;
private List<Course> courses;
}
public class Course {
private String name;
private Date startDate;
private Date endDate;
}
@Path("/students")
public class StudentService {
@GET
@Path("/{id}")
@Produces("application/json")
public Response getStudentDetails(@PathParam("id") int studentId) {
ResponseBuilder builder = Response.status(200);
//Just sending back the id for now
return builder.entity(studentId+"").build();
}
}
@Path("/students/{id}/courses")
public class CourseService {
@GET
@Path("/{name}")
@Produces("application/json")
public Response getCourseDetails(@PathParam("name") String courseName) {
ResponseBuilder builder = Response.status(200);
//Just sending back the name for now.
return builder.entity(courseName).build();
}
}
公共班级学生{
私有int-id;
私有字符串名称;
私人名单课程;
}
公共课{
私有字符串名称;
私人日期开始日期;
私人日期结束日期;
}
@路径(“/学生”)
公共班级学生服务{
@得到
@路径(“/{id}”)
@生成(“应用程序/json”)
公共响应getStudentDetails(@PathParam(“id”)int studentId){
ResponseBuilder=Response.status(200);
//现在就把身份证寄回去
返回builder.entity(studentId+“”).build();
}
}
@路径(“/students/{id}/courses”)
公共课程服务{
@得到
@路径(“/{name}”)
@生成(“应用程序/json”)
公共响应getCourseDetails(@PathParam(“名称”)字符串courseName){
ResponseBuilder=Response.status(200);
//现在就把名字寄回去。
返回builder.entity(courseName.build();
}
}
代码运行良好,但CourseService的路径看起来不正确。是否存在可以从StudentService调用的重定向?
另外,REST中是否有只检索学生姓名而不是整个对象的选项?我认为您应该区分这两个服务/资源。 要获取学生详细信息,请使用:/students/{id} 要获取课程详细信息,请使用:/courses/{name}
我认为您应该区分这两种服务/资源。 要获取学生详细信息,请使用:/students/{id} 要获取课程详细信息,请使用:/courses/{name}
请出示您的代码:您尝试了什么?哪里出了问题?你能找到什么工作?看,我很抱歉我问这个问题的方式。使用样例源代码编辑。代码运行良好,但CourseService的路径让我担心。这是正确的实现方式吗?请展示您的代码:您尝试了什么?哪里出了问题?你能找到什么工作?看,我很抱歉我问这个问题的方式。使用样例源代码编辑。代码运行良好,但CourseService的路径让我担心。这是正确的实现方式吗?当然。我会区分服务。对于具有多层层次结构的依赖服务,如@Path(“/{studentId}/courses/{coursename}/x/{xname]/y/{yname}”),所有这些方法都必须在父服务中定义,对吗?使代码变得笨拙。或者应该对层次结构级别进行限制吗?是的,您可以实现从属服务。我将用一个示例更新答案。我认为您可以拥有的子资源数量没有限制。但是,如果层次结构变得太长,您就没有限制我想知道这个服务是否设计得很好当然。我会区分服务。对于具有多层次结构的依赖服务,如@Path(“/{studentId}/courses/{coursename}/x/{xname]/y/{yname}”),所有这些方法都必须在父服务中定义,对吗?使代码变得笨拙。或者应该对层次结构级别进行限制吗?是的,您可以实现从属服务。我将用一个示例更新答案。我认为您可以拥有的子资源数量没有限制。但是,如果层次结构变得太长,您就没有限制不知道这项服务是否设计得很好
@Path("/courses")
public class CourseRes {
@Inject
CourseService myCourseService;
@GET
@Path("/{courseName}")
public Course get(@PathParam(value="courseName") String courseName){
Course course = myCourseService.getCourse(courseName);
return course;
}
}
@Path("/students")
public class StudentRes {
@Inject
StudentService myStudentService;
@GET
@Path("/{studentId}")
public Student get(@PathParam(value="studentId") String studentId){
Student student = myStudentService.getStudent(studentId);
return student;
}
@GET
@Path("/{studentId}/courses")
public List<Course> get(@PathParam(value="studentId") String studentId){
List<Course> courses = myStudentService.getCourseByStudentId(studentId);
return courses;
}
}
@Path("/students")
public class StudentRes {
@Path("{studentId}/courses")
public CourseStudentRes getCourseStudentRes(
@PathParam("studentId") int studentId,
@Context ResourceContext rc) {
return rc.initResource(new CourseStudentRes(studentId));
}
}
@RequestScoped
@Produces("application/json")
public class CourseStudentRes {
private int studentId;
public CourseStudentRes(int studentId) {
this.studentId = studentId;
}
@Path("{courseName}/x")
public XCourseStudent getXCourseStudentRes(
@PathParam("courseName") String courseName,
@Context ResourceContext rc) {
return rc.initResource(new XCourseStudent(studentId,courseName));
}
@RequestScoped
@Produces("application/json")
public class XCourseStudentRes {
private int studentId;
private String courseName;
public XCourseStudentRes(int studentId, String courseName) {
this.studentId = studentId;
this.courseName = courseName;
}
@Path("{xName}/y")
public YXCourseStudentRes getYXCourseStudentRes(
@PathParam("xName") String xName,
@Context ResourceContext rc) {
return rc.initResource(new YXCourseStudentRes(studentId,courseName,xName));
}
}