HTTP 状态 400 - 错误请求 - 休眠
Posted
技术标签:
【中文标题】HTTP 状态 400 - 错误请求 - 休眠【英文标题】:HTTP Status 400 – Bad Request - Hibernate 【发布时间】:2020-05-23 15:58:41 【问题描述】:我对 hibernate 和 Spring MVC 还是很陌生。我在演示 Web 应用程序上尝试休眠和 Spring MVC。问题是当我去编辑页面并在我点击保存后点击保存按钮时,我得到“HTTP 状态 400 -Bad Request”。如果你们能帮助我,我将不胜感激。
这是我的 index.jsp:(其中有一个带有编辑和删除按钮的操作列)
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>CUSTOMER MANAGER</title>
</head>
<body>
<div align="center">
<h2>Customer Manager</h2>
<form method="get" action="search">
<input type="text" name="keyword" />
<input type="submit" value="Search" />
</form>
<br>
<form action="new">
<button type="submit" >New Customer</button>
</form>
<br>
<hr>
<br>
<table border="1" cellpadding="5">
<tr>
<th>ID</th>
<th>Name</th>
<th>E-mail</th>
<th>Address</th>
<th>Action</th>
</tr>
<c:forEach items="$listCustomer" var="customer">
<tr>
<td>$customer.id</td>
<td>$customer.name</td>
<td>$customer.email</td>
<td>$customer.address</td>
<td>
<form action="edit/$customer.id" method="post">
<button type="submit" >Edit</button>
</form>
<form action="delete/$customer.id" method="post">
<button type="submit">Delete</button>
</form>
</td>
</tr>
</c:forEach>
</table>
</div>
</body>
</html>
这是我的 edit_customer.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Edit Customer</title>
</head>
<body>
<div align="center">
<h2>Edit Customer</h2>
<form:form action="save" method="post" modelAttribute="customer">
<table border="0" cellpadding="5">
<tr>
<td>ID:</td>
<td>$ customer.id </td>
<form:hidden path = "id"></form:hidden>
</tr>
<tr>
<td>Name: </td>
<td><form:input path="name" /></td>
</tr>
<tr>
<td>Email: </td>
<td><form:input path="email" /></td>
</tr>
<tr>
<td>Address: </td>
<td><form:input path="address" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Save"></td>
</tr>
</table>
</form:form>
</div>
</body>
</html>
这是我的 CustomerController.java:
package net.codejava.customer;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class CustomerController
@Autowired
private CustomerService service;
@RequestMapping("/")
public ModelAndView home()
ModelAndView mav = new ModelAndView("index");
List<Customer> listCustomer = service.listAll();
mav.addObject("listCustomer", listCustomer);
return mav;
@RequestMapping("/new")
public String newCustomerForm(Map<String, Object> model)
model.put("customer", new Customer());
return "new_customer";
@RequestMapping(value = "/save", method = RequestMethod.POST )
public String saveCustomer(@ModelAttribute("customer") Customer customer)
service.save(customer);
return "redirect:/";
@RequestMapping(value="/edit/id" , method = RequestMethod.POST )
public ModelAndView editCustomer(@PathVariable(value="id") Long id)
ModelAndView mav = new ModelAndView("edit_customer");
Customer customer = service.get(id);
mav.addObject("customer", customer);
return mav;
@RequestMapping(value="/delete/id" , method = RequestMethod.POST )
public String deleteCustomerForm(@PathVariable(value="id") Long id)
service.delete(id);
return "redirect:/";
@RequestMapping("/search")
public ModelAndView search(@RequestParam String keyword)
ModelAndView mav = new ModelAndView("search");
List<Customer> listCustomer = service.search(keyword);
mav.addObject(listCustomer);
return mav;
这是我的 CustomerService.java
package net.codejava.customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class CustomerService
@Autowired CustomerRepository repo;
public void save(Customer customer)
repo.save(customer);
public List<Customer> listAll()
return (List<Customer>) repo.findAll();
public void delete(Long id)
repo.deleteById(id);
public Customer get(Long id)
Optional<Customer> result = repo.findById(id);
return result.get();
public List<Customer> search(String keyword)
return repo.search(keyword);
这是我的实体类 Customer.java
package net.codejava.customer;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Customer
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String email;
private String address;
public Customer()
public Long getId()
return id;
public void setId(Long id)
this.id = id;
public String getName()
return name;
public void setName(String name)
this.name = name;
public String getEmail()
return email;
public void setEmail(String email)
this.email = email;
public String getAddress()
return address;
public void setAddress(String address)
this.address = address;
【问题讨论】:
你能在service.save(customer);
附近添加日志吗?可能有问题。还要检查你的"redirect:/"
,它有效吗?
@Boris 是的,我在我的新客户添加页面上也使用了“redirect:/”,所以那里没有问题。但我会看看日志记录。很抱歉我可能回复慢,因为我以前从未使用过日志记录。
请将你的项目推送到 gihub 并在这里分享。为您提供帮助会更轻松
@Boris 很抱歉我无法进行日志记录,你能给我举个例子吗
@Achille_vanhoutte github.com/BusraSunar/Hibernate 这是我的 GitHub 存储库的链接。
【参考方案1】:
很抱歉浪费了您的时间,但我已经解决了我的问题。原因是我的映射。我应该在我的请求映射中完成 /edit/save 。但我真的很感谢你们帮助我。非常感谢
【讨论】:
【参考方案2】:mysql 端口应该是 3306 而不是 3308 (persistence.xml)
您的 CustomerRepository 接口必须标有 @Repository
@Repository
public interface CustomerRepository extends CrudRepository<Customer, Long>
【讨论】:
我正在使用 wamp 服务器,所以我使用 3308 端口。在编辑页面上点击保存时我仍然收到 400 Bad request 我不知道 wamp。根据您的 persistence.xml 文件,我假设您的数据库是 Mysql。无论如何,它适用于 Mysql 和 Tomcat 8。github.com/Georges73/hibernate 我查看了您的 GitHub 存储库,但没有发现很多差异。我认为您更改了 persistence.xml,但这对我来说没有任何改变。嗯,非常感谢,我真的很感激。以上是关于HTTP 状态 400 - 错误请求 - 休眠的主要内容,如果未能解决你的问题,请参考以下文章
ResponseEntity:HTTP状态400 - 错误请求