Spring引导REST API缺少URI模板变量

时间:2018-01-10 16:07:04

标签: java rest api maven spring-boot

我已经跟随this tutorial使用Spring启动构建REST API。它教了很多。但是我想要做的确让我陷入困境。 我想要得到的是:

{
     "marks":{
          "id":"1",
          "name":"test",
          "remark":"passed",
          "course": {
              "id": "1",
              "name": "Spring Boot",
              "description": "Solves many problems",
              "topic": {
                 "id": "1",
                 "name": "Java",
                 "description": "Powerful Programming Language"
    }
}

但是当我尝试添加标记时,我收到了错误:

{
    "timestamp": 1515600105327,
    "status": 500,
    "error": "Internal Server Error",
    "exception": "org.springframework.web.bind.MissingPathVariableException",
    "message": "Missing URI template variable 'courseId' for method parameter of type String",
    "path": "/topics/1/courses/1/marks"
}

我的标记模型是:

public class Marks {

    @Id
    private String id;

    private String name;
    private String remark;

    @ManyToOne
    private Course course;

    @ManyToOne
    private Topic topic;

    public Marks() {

    }

    public Topic getTopic() {
        return topic;
    }

    public void setTopic(Topic topic) {
        this.topic = topic;
    }

    public Marks(String id, String name, String remark,String topicId, String courseId) {
        this.id = id;
        this.name = name;
        this.remark = remark;
        this.topic = new Topic(topicId, "","");
        this.course = new Course(courseId, " ", " ", " ");
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public Course getCourse() {
        return course;
    }

    public void setCourse(Course course) {
        this.course = course;
    }
}

MarksController.java

public class MarksController {

    @RestController
    public class MarksController {

    @Autowired
    private MarksService marksService;

    @RequestMapping("/topics/{topicId}/courses/{id}/marks")
    public List<Marks> getAllMarks(@PathVariable String courseId) {
        return marksService.getAllMarks(courseId);
    }

    @RequestMapping(method=RequestMethod.POST, value="/topics/{topicId}/courses{courseId}/marks")
    public void addMarks(@RequestBody Marks marks,@PathVariable String topicId ,@PathVariable String courseId) {
        marks.setTopic(new Topic(topicId, "", ""));
        marks.setCourse(new Course(courseId, "", "", ""));
        marksService.addMarks(marks);
    }
}

MarksService.java

public class MarksService {

    @Service
    public class MarksService {

    @Autowired
    private MarksRepository marksRepository;

    public void addMarks(Marks marks) {
        marksRepository.save(marks);
    }
}

MarksRepository.java

public interface MarksRepository extends CrudRepository<Marks, String> {

    public List<Marks> findByCourseId(String courseId);

    public List<Marks> findByTopicId(String topicId);

}

任何人都可以帮助我获得上述JSON中的结果。

1 个答案:

答案 0 :(得分:2)

对于POST方法

此:

/topics/{topicId}/courses{courseId}/marks

应该是:

/topics/{topicId}/courses/{courseId}/marks

请注意courses{courseId}

之间的附加/

对于GET方法

此:

/topics/{topicId}/courses/{id}/marks

应该是:

/topics/{topicId}/courses/{courseId}/marks

请注意使用courseId同意MarksController.getAllMarks中的参数名称。