表单数据不是从浏览器提交在邮递员工作 [英] Form data submit working in postman not from browser

查看:167
本文介绍了表单数据不是从浏览器提交在邮递员工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用邮递员工具向以下网址提交邮寄请求 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
公共类消费者{

@Autowired
私人PaideUserService paideUserService;




@RequestMapping(/ consumer)
public List< PaideUser> getAllUsers(@RequestParam(email)字符串电子邮件){
return paideUserService.getOneUser(email);


$ b @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 =postaction =consumerenctype =multipart / form-data> ; 
< input type =textname =firstName>
< input type =textname =lastName>
< input type =textname =email>
< input type =textname =address>
< input type =textname =zipCode>
< input type =textname =phone>
< input type =submit>
< / form>

提交时我目前看到以下错误:

 出现意外错误(type = Bad Request,status = 400)。 
对象='paideUser'的验证失败。错误计数:1

但是,正如我之前告诉过的,如果我从邮递员处发送邮件请求。我不知道我的表单或我的控制器是否有问题,有人请帮助我,任何帮助或建议将不胜感激。



My 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;
私人字符串电子邮件;
私有字符串地址;
私人字符串zipCode;
私人手机;

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 =电子邮件;
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;
}


}


解决方案

我重现了您的环境,并以适当的价值观工作。你的问题很可能是手机领域。因为它不是 int ,所以它不能保存文本,一个空值或一个大于 Integer.MAX_VALUE 。它也不能用来保存典型的手机号码。更好地使用 String 相关问题



我建议您打开浏览器的DevTools,选择Network选项卡,然后查看您的POST请求看起来(标题和所有),并尝试与邮递员请求进行比较。



另外,中间名中没有表格。可能不是问题,但我认为我仍然指出。


I am using postman tool to submit a post request to the following url "http://localhost:8080/myapp/consumer" and I am not setting any header, but still my application is able to read it and consume and display the expected output successfully but when I am doing this by using a html form from a jsp page it is not working can some one please help me on this, I have tried so many solutions but nothing work, below is my code.

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");

    }

}

my html code looks like this

<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>

when I submit I have seen the following errors so far

There was an unexpected error (type=Bad Request, status=400).
Validation failed for object='paideUser'. Error count: 1

But as I told earlier it was working fine if I do the post request from postman. I don't know whether I am wrong with my form or my controller, someone please help me, any help or suggestions would be greatly appreciated.

My PaideUser looks like

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;
    }


}

解决方案

I reproduced your environment and it works with proper values. Most likely your problem is the phone field. Because it isn't an int it can't hold text, an empty value, or a number more than Integer.MAX_VALUE. It also can't be used to hold a typical mobile phone number. Better use String Related question

I'd advise you to open the DevTools of your browser, select the Network tab, and see how exactly your POST request looks (headers and all) and try to compare that with what postman requests.

Also, there's no middleName in the form. Probably not an issue, but I thought I'd still point it out.

这篇关于表单数据不是从浏览器提交在邮递员工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆