gpt4 book ai didi

java - Spring boot REST API 缺少 URI 模板变量

转载 作者:行者123 更新时间:2023-12-02 02:23:14 25 4
gpt4 key购买 nike

我已关注this tutorial使用 Spring boot 构建 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 中的结果。

最佳答案

对于POST方法

这个:

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

应该是:

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

注意courses{courseId}之间的附加/

对于 GET 方法

这个:

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

应该是:

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

请注意使用 courseIdMarksController.getAllMarks 中的参数名称一致。

关于java - Spring boot REST API 缺少 URI 模板变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48191536/

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