【发布时间】:2019-11-18 02:19:52
【问题描述】:
我正在尝试在STS 3 中创建一个简单的Spring Boot application,用户可以在其中注册并创建他们的帐户。我正在使用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”
【问题讨论】:
-
您是否尝试过为您的请求映射注释设置
consumes属性以指示您接受哪些媒体类型? -
不,我不知道。你能解释一下吗。 @迈克
-
尝试将
consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE添加到您的@RequestMapping注释中。 -
试过但没用。 @迈克
-
还是 415 响应?
标签: java spring spring-boot jsp