我正在尝试在Spring Boot application
中创建一个简单的STS 3
,用户可以在其中注册并创建他们的帐户。我正在将PostgreSQL
数据库和JPA
与休眠一起使用。以下是代码。
实体类:
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
似乎单击提交后,控件永远不会进入控制器,因为--- 2 ---永远不会输出到控制台,即使表单中的操作与请求映射相同。
显示的错误是:
发生意外错误(类型=不支持的介质类型,状态= 415)。 内容类型'application / x-www-form-urlencoded; charset = UTF-8'不支持
答案 0 :(得分:0)
确保您正在调用在控制器中声明的确切端点(localhost:port / register)。如果您未正确调用,则可能会输出HTTP 404错误...检查该错误。
如果没有任何HTTP错误,我将注释更改为这种格式(并将调用相应地更改为/ register / user)(如Spring指南here所示):
@RestController
@RequestMapping("/register")
public class UserAccountController {
@Autowired
UserAccountService userAccountService;
@PostMapping("/user")
public void registerAccount(@RequestBody UserAccount user) {
System.out.println("------2------");
System.out.println(user.getfName());
userAccountService.register(user);
}
}