当我尝试运行一个简单的spring boot mysql crud操作时,我正在接收一个阻止程序。要给你一个要点,我有一个简单的jsp主页,其中有一个注册按钮,该按钮导航到具有一个表单的注册页面文本框和一个称为提交的按钮。单击提交按钮后,表单中的值应被推送到数据库。当我尝试单击注册页面中的“提交”按钮时,我面临的问题是错误。请看一下代码以更好地理解。
我正在进行后映射,并尝试使用repository.save()克鲁德操作(使用@requestbody批注创建对象)来保存数据。但是这样做给了我以下错误
"There was an unexpected error (type=Unsupported Media Type, status=415).
Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported".
控制器:
package com.kaushik.Megamart.Controllers;
import java.awt.PageAttributes.MediaType;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
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.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import com.kaushik.Megamart.model.Register;
import com.kaushik.Megamart.repository.RegisterRepository;
@Controller
public class HomeController {
@Autowired
private RegisterRepository repository;
@GetMapping("/")
public String homepage()
{
System.out.println("In Home page");
return "home.jsp";
}
@GetMapping("/register")
public String registerpage()
{
System.out.println("In register page");
return "regist.jsp";
}
@RequestMapping(path ="/regi")
public String savedata(@Valid Register ex) {
repository.save(ex);
System.out.println("Entered function");
return "success.jsp";
}
}
POJO(Register.java):
package com.kaushik.Megamart.model;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotEmpty;
@Entity
@Table(name="account")
public class Register {
@Id
@NotEmpty
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
regist.jsp:
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<title>Registration</title>
</head>
<body>
<form action="/regi" method="post">
<table>
<tr>
<td>User Name :</td>
<td><input type="text" name="name"/></td>
</tr>
</table>
<input type="submit" value="Submit">
</form>
</html>
RegisterRepository.java:
package com.kaushik.Megamart.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.CrudRepository;
import com.kaushik.Megamart.model.Register;
public interface RegisterRepository extends JpaRepository<Register, String> {
}
预期:我希望将注册表中的数据推送到数据库中。
实际:在注册页面中单击“提交”按钮时收到错误。
Error :There was an unexpected error (type=Internal Server Error, status=500).
could not execute statement; nested exception is org.hibernate.exception.GenericJDBCException: could not execute statement