Form data submit working in postman not from brows

2019-08-27 00:48发布

问题:

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


}

回答1:

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.