To respond with an HTTP 400 error in a Spring MVC controller method that returns a String, you can throw a ResponseStatusException with a status of BAD_REQUEST.
Here is an example of how you can do this:
@ResponseBody
@GetMapping("/some-url")
public String someMethod() {
// some logic here
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Invalid request");
}
In this example, the ResponseStatusException is thrown with a status of BAD_REQUEST (HTTP 400) and a message. The message will be included in the response body.
Alternatively, you can use the HttpServletResponse object to set the response status and write the response body manually:
Good day. I faced a problem: I'm trying to send POST request with some attributes, but I get "Bad request" response.
Here is my controller:
@Controller
@RequestMapping("/group")
public class GroupController {
private static org.apache.log4j.Logger logger = org.apache.log4j.Logger
.getLogger(GroupController.class);
@Autowired
private GroupService groupService;
@RequestMapping(value = "/add", method = RequestMethod.POST)
public String addGroup(@ModelAttribute("group") final Group group) {
GroupController.logger.info("I'm in POST method");
this.groupService.addGroup(group);
return "redirect:/student/add";
}
}
Here is my entity:
@Entity
@Table(name = "university_groups")
public class Group implements Serializable {
private static final long serialVersionUID = 1L;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "group")
Set students;
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@NotNull
@JoinColumn(name = "department_id")
private Department department;
@Id
@Column(name = "group_name", unique = true, nullable = false)
private String group;
public Group() {
}
public Group(final String group, final Department deparment) {
this.group = group;
this.department = deparment;
}
public Department getDepartment() {
return this.department;
}
public String getGroup() {
return this.group;
}
public Set getStudents() {
return this.students;
}
public void setDepartment(final Department department) {
this.department = department;
}
public void setGroup(final String group) {
this.group = group;
}
public void setStudents(final Set students) {
this.students = students;
}
@Override
public String toString() {
return this.group;
}
}
Here is part of JSP page:
">
" />
Here is Department entity:
@Entity
@Table(name = "departments")
public class Department implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "department", unique = true, nullable = false)
private String department;
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@NotNull
@JoinColumn(name = "faculty_id")
private Faculty faculty;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "department")
private Set groups;
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "users_departments", joinColumns = {@JoinColumn(name = "department_id", nullable = false, insertable = true, updatable = true) }, inverseJoinColumns = {@JoinColumn(name = "user_name", nullable = false, insertable = true, updatable = true) })
private Set users;
public Department() {
}
public Department(final String department, final Faculty faculty) {
this.department = department;
this.faculty = faculty;
}
public String getDepartment() {
return this.department;
}
public Faculty getFaculty() {
return this.faculty;
}
public Set getGroups() {
return this.groups;
}
public Set getUsers() {
return this.users;
}
public void setDepartment(final String department) {
this.department = department;
}
public void setFaculty(final Faculty faculty) {
this.faculty = faculty;
}
public void setGroups(final Set groups) {
this.groups = groups;
}
public void setUsers(final Set users) {
this.users = users;
}
@Override
public String toString() {
return this.department;
}
}
If I remove @ModelAttribute("group") final Group group from controller method, all is OK. Please, help me, I can't understand why it doesn't works.