【问题标题】:Form data submit working in postman not from browser表单数据提交在邮递员中工作,而不是从浏览器
【发布时间】: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


【解决方案1】:

我复制了您的环境,它适用于正确的值。您的问题很可能是电话领域。因为它不是int,所以它不能包含文本、空值或大于Integer.MAX_VALUE 的数字。它也不能用于保存典型的手机号码。更好用StringRelated question

我建议您打开浏览器的 DevTools,选择“网络”选项卡,查看您的 POST 请求的外观(标头和所有内容)并尝试将其与邮递员的请求进行比较。

此外,表单中没有middleName。可能不是问题,但我想我还是会指出这一点。

【讨论】:

  • 您好,效果很好,我已将 phone 变量从 int 更改为 String,非常感谢,您真是个天才。
猜你喜欢
  • 2019-10-10
  • 2020-07-10
  • 1970-01-01
  • 2020-10-23
  • 2020-05-18
  • 1970-01-01
  • 2017-06-26
  • 2019-10-02
  • 2014-10-09
相关资源
最近更新 更多