I have followed this tutorial to build REST API using Spring boot. It taught alot. But What I am trying to do really got me stuck. What I am trying to get is:
{
"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"
}
}
But I get the error when I tried to add the marks- as :
{
"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"
}
My Marks Model is:
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;
}
}
And 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);
}
}
And MarksService.java
:
public class MarksService {
@Service
public class MarksService {
@Autowired
private MarksRepository marksRepository;
public void addMarks(Marks marks) {
marksRepository.save(marks);
}
}
And MarksRepository.java
:
public interface MarksRepository extends CrudRepository<Marks, String> {
public List<Marks> findByCourseId(String courseId);
public List<Marks> findByTopicId(String topicId);
}
Can anyone help me get the result as in the mentioned JSON.
POST
method This:
/topics/{topicId}/courses{courseId}/marks
Should be:
/topics/{topicId}/courses/{courseId}/marks
Note the additional / between courses
and {courseId}
GET
method This:
/topics/{topicId}/courses/{id}/marks
Should be:
/topics/{topicId}/courses/{courseId}/marks
Note the use of courseId
to agree with the parameter name in MarksController.getAllMarks
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.