java - 如何调用依赖的rest服务

标签 java rest jax-rs resteasy

我正在使用rest easy构建休息服务。有两种资源——学生和类(class),以及学生和类(class)之间的 1:N 关系。 要获取学生详细信息:网址为/students/1 获取类(class)详细信息:/students/1/courses/science。

源代码:

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

    }

}

代码工作正常,但 CourseService 的路径看起来不正确。是否有可以从 StudentService 调用的任何重定向? 另外,REST 中是否有一个选项,可以仅检索学生的姓名而不是整个对象?

最佳答案

我认为你应该区分这两种服务/资源。 要获取学生详细信息,请使用:/students/{id} 要获取类(class)详细信息,请使用:/courses/{name}

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

}

仅检索学生的姓名而不是整个对象并不是最佳实践。 无论如何,StudentService 提供的对象只能通过名称来增强。这取决于你的 内部实现。

更新:

对于具有多级层次结构的依赖服务,例如@Path("/{studentId}/courses/{coursename}/x/{xname]/y/{yname}"),您可以使用 ResourceContext使用路径中的 ids 初始化子资源实例。

但是您必须注意这些子资源依赖于父资源。

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

关于java - 如何调用依赖的rest服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36062787/

相关文章:

Java:强制实现接口(interface)以扩展抽象类

java - 如何使用 apache POI 从 Excel 中删除图表?

java - Alluxio master动态地有详细的输出是正常的吗

database - 如何使用 put 方法从表单更新数据库

java - 将返回类型从列表更改为响应后,RestRessource 不再工作

java - 如何将 Java 反射与通用参数一起使用

json - 在 SoapUI 中将属性作为 JSON 传递

java - JAX-RS POST bean 属性的默认值

java - @Consumes(MediaType.APPLICATION_FORM_URLENCODED) FormParameter 为空

java - 允许斜杠的 JAX-RS @Path 与其他资源发生冲突