<分区>
我正在尝试在 STS 3
中制作一个简单的 Spring Boot 应用程序
,用户可以在其中注册并创建他们的帐户。我正在使用 PostgreSQL
数据库和 JPA
hibernate 。以下是代码。
实体类:
package tv.app.user;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class UserAccount {
@Id
@GeneratedValue
private Long uid;
private String username;
private String email;
private String password;
public long getUid() {
return uid;
}
public void setUid(long uid) {
this.uid = uid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
控制者:
package tv.app.user;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class UserAccountController {
@Autowired
UserAccountService userAccountService;
@RequestMapping(value="/register", method=RequestMethod.POST)
public void registerAccount(@RequestBody UserAccount user) {
System.out.println("------2------");
System.out.println(user.getfName());
userAccountService.register(user);
}
}
存储库:
package tv.app.user;
import org.springframework.data.repository.CrudRepository;
public interface UserAccountRepository extends CrudRepository<UserAccount, Long> {
}
商务服务类:
package tv.app.user;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserAccountService {
@Autowired
private UserAccountRepository userRepository;
public void register(UserAccount user) {
userRepository.save(user);
}
}
Jsp 表单:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="/register" method="post">
<input type="text" name="username" placeholder="Username" required /><br>
<input type="text" name="email" placeholder="Email ID" required /><br>
<input type="password" name="password" placeholder="Password" required /><br>
<input type="submit" value="Signup" />
</form>
</body>
</html>
application.properties 文件:
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
spring.jpa.hibernate.ddl-auto=create
spring.datasource.initialization-mode=always
spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://localhost:5432/myApp
spring.datasource.username=postgres
spring.datasource.password=postgres
似乎在点击提交后,控件永远不会进入 Controller ,因为 ---2--- 永远不会输出到控制台,即使表单中的操作与请求映射相同。
显示的错误是:
There was an unexpected error (type=Unsupported Media Type, status=415). Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported