Hello there! I have a query regarding the usage of @RequestParam in @RestController. My question is about extracting @RequestParam from the client side. Below is an example of server code using @RestController:
@ResponseBody
@RequestMapping(method = RequestMethod.GET)
public ResponseEntity<List<ProjectBean>> getAllBeans(@RequestParam(name = "head") Integer headId) {
Integer head = securityService.getLoggedAccountId();
List<ProjectBean> projects = (List<ProjectBean>) projectService.getByHead(head);
return new ResponseEntity<List<ProjectBean>>(projects, HttpStatus.OK);
}
And here is the JSP/JavaScript code:
function loadProjects() {
$.ajax({
url : 'rest/projects',
method : 'GET',
headers : {
'Content-Type' : 'application/json',
},
success: function(data){
$.each(data, function(index, project) {
addProject(project);
});
}
});
}
This function fetches all projects, but does not filter them by the exact headId.
Let's take a look at the entity structure:
@Entity
@Table(name = "projects")
public class Project {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "project_name", nullable = false, length = 255)
private String projectName;
@Column(name="head")
private Integer head; //need to get projects with this value
@Column(name = "description")
private String description;
@Column(name = "photo")
private String photo;
@Column(name = "status", nullable = false, length = 200)
private String status;
}