【发布时间】:2018-07-11 06:17:54
【问题描述】:
我正在使用邮递员工具向以下网址“http://localhost:8080/myapp/consumer”提交发布请求,并且我没有设置任何标题,但我的应用程序仍然能够读取它并成功使用并显示预期的输出但是当我我是通过使用 jsp 页面中的 html 表单来做到这一点的,它不起作用,有人可以帮我解决这个问题,我尝试了很多解决方案,但没有任何效果,下面是我的代码。
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
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.RestController;
import com.nonprofit.charity.nonprofit.databaseoperations.PaideUserService;
import com.nonprofit.charity.nonprofit.entity.PaideUser;
@RestController
public class Consumer {
@Autowired
private PaideUserService paideUserService;
@RequestMapping("/consumer")
public List<PaideUser> getAllUsers(@RequestParam("email") String email){
return paideUserService.getOneUser(email);
}
@RequestMapping(method=RequestMethod.POST,value="/consumer")
public void somethingNew(@ModelAttribute PaideUser user) {
System.out.println("This is FirstName: "+user);
paideUserService.addNewRow(user);
System.out.println("Success");
}
}
我的html代码是这样的
<form method="post" action="consumer" enctype="multipart/form-data">
<input type="text" name="firstName">
<input type="text" name="lastName">
<input type="text" name="email">
<input type="text" name="address">
<input type="text" name="zipCode">
<input type="text" name="phone">
<input type="submit">
</form>
当我提交时,到目前为止我看到了以下错误
There was an unexpected error (type=Bad Request, status=400).
Validation failed for object='paideUser'. Error count: 1
但正如我之前所说,如果我从邮递员那里发出帖子请求,它会正常工作。我不知道我的表单或控制器是否有问题,请有人帮助我,任何帮助或建议将不胜感激。
我的 PaideUser 看起来像
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
@Entity
public class PaideUser {
@javax.persistence.Id
@GeneratedValue
private Integer Id;
private String firstName;
private String middleName;
private String lastName;
private String email;
private String address;
private String zipCode;
private int phone;
protected PaideUser() {
}
public PaideUser(Integer Id, String firstName, String middleName,String lastName, String email, String address, String zipCode, int phone) {
this.Id=Id;
this.firstName=firstName;
this.middleName=middleName;
this.lastName=lastName;
this.email=email;
this.address=address;
this.zipCode=zipCode;
this.phone=phone;
}
public Integer getId() {
return Id;
}
public void setId(Integer id) {
Id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getMiddleName() {
return middleName;
}
public void setMiddleName(String middleName) {
this.middleName = middleName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
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;
}
public String getZipCode() {
return zipCode;
}
public void setZipCode(String zipCode) {
this.zipCode = zipCode;
}
public int getPhone() {
return phone;
}
public void setPhone(int phone) {
this.phone = phone;
}
}
【问题讨论】:
-
enctype="multipart/form-data"- 为什么? -
我也试过没有它但没有用,我试过 text/plain、multipart/form-data 和 application/json 都没有工作。
-
我还以为你不需要任何东西。
-
PaideUser长什么样子? -
顺便说一句,您的
postman请求真的转到POST方法吗?如果不是,则不相关
标签: java html spring forms spring-boot