Spring REST 400 Bad Request with x-www-urlformencoded POST of Body to @ModelAttribute Domain Object
Spring REST 400 Bad Request with x-www-urlformencoded POST of Body to @ModelAttribute Domain Object
Hi, I am reaching out in the hopes that I am overlooking something trivial. I don't see any problem with the url encodning post here. I can't get the details of my 400 Bad Request exception message despite my trying to catch it. It is possibly a class I am not mapping to in my handler.
Anyway, this works with a plain application/json post, but has problems with the posting via url encoding. I am not even reaching my controller , therefore I am fairly sure that this is a binding problem to my sample entity when url encoding is involved. It binds fine when I use @RequestBody and post using application/json, but has problems with @ModelAttribute when posting via urlencoding. What is it about my body for the urlencoding post that is a problem?
Bulk Edit Text
firstName:JohnTest
lastName:Stafford
birthDate:1990-08-07
driversLicenseNumber:080005900
address:1700NW36thTerraceYukonOklahoma
zip:73099
email:john.charles.stafford@gmail.com
homePhone:4055506800
workPhone:4055506800
amount:300
employer:meMyselfAndI
activeMilitary:0
incomeType:EMPLOYMENT
monthlyIncome:12000
date1:2016-12-02
date2:2016-12-16
frequency:TWICEMONTHLY
Controller method used for the x-www-formurlencoded POST
@RequestMapping(method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
@ResponseBody
public Lead save(@Valid @ModelAttribute("entity") final SampleEntity entity)
{
return createInternal(entity);
}
SampleEntity (again, this works for an application/json POST)
@Entity
@Table(name="sample_table")
public class SampleEntity implements IBaseEntity{
/**
*
*/
private static final long serialVersionUID = -1110216193971231355L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "first_name")
@NotNull
private String firstName;
@Column(name = "last_name")
@NotNull
private String lastName;
@Column(name = "birth_date")
@NotNull
private String birthDate;
@Column(name = "drivers_license_number")
@NotNull
private String driversLicenseNumber;
@Column
@NotNull
private String address;
@Column
@NotNull
private String zip;
@Column
@NotNull
private String email;
@Column(name = "home_phone")
@NotNull
private String homePhone;
@Column(name = "work_phone")
@NotNull
private String workPhone;
@Column(name = "requested_amount")
@NotNull
private String requestedAmount;
@Column
@NotNull
private String employer;
@Column(name = "active_military")
@NotNull
private String activeMilitary;
@Column(name = "income_type")
@NotNull
private String incomeType;
@Column(name = "monthly_income")
@NotNull
private String monthlyIncome;
@Column
@Temporal(TemporalType.DATE)
@NotNull
private Date date1;
@Column
@Temporal(TemporalType.DATE)
@NotNull
private Date date2;
@Column(name = "frequency")
@NotNull
private String frequency;
//getters and setters
Any help greatly appreciated. Thanks.
尝试,
@RequestMapping( method = RequestMethod.POST, consumes = {"application/x-www-form-urlencoded", "application/json"})
必须将 BindingResult 添加到我的参数中才能发现问题。这样做之后,我很明显 ModelAttribute 无法绑定来自我 post 的日期字符串并将其转换为 java.util.Date 。需要将 InitBinding 自定义转换器添加到我的控制器 class。
Hi, I am reaching out in the hopes that I am overlooking something trivial. I don't see any problem with the url encodning post here. I can't get the details of my 400 Bad Request exception message despite my trying to catch it. It is possibly a class I am not mapping to in my handler.
Anyway, this works with a plain application/json post, but has problems with the posting via url encoding. I am not even reaching my controller , therefore I am fairly sure that this is a binding problem to my sample entity when url encoding is involved. It binds fine when I use @RequestBody and post using application/json, but has problems with @ModelAttribute when posting via urlencoding. What is it about my body for the urlencoding post that is a problem?
Bulk Edit Text
firstName:JohnTest
lastName:Stafford
birthDate:1990-08-07
driversLicenseNumber:080005900
address:1700NW36thTerraceYukonOklahoma
zip:73099
email:john.charles.stafford@gmail.com
homePhone:4055506800
workPhone:4055506800
amount:300
employer:meMyselfAndI
activeMilitary:0
incomeType:EMPLOYMENT
monthlyIncome:12000
date1:2016-12-02
date2:2016-12-16
frequency:TWICEMONTHLY
Controller method used for the x-www-formurlencoded POST
@RequestMapping(method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
@ResponseBody
public Lead save(@Valid @ModelAttribute("entity") final SampleEntity entity)
{
return createInternal(entity);
}
SampleEntity (again, this works for an application/json POST)
@Entity
@Table(name="sample_table")
public class SampleEntity implements IBaseEntity{
/**
*
*/
private static final long serialVersionUID = -1110216193971231355L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "first_name")
@NotNull
private String firstName;
@Column(name = "last_name")
@NotNull
private String lastName;
@Column(name = "birth_date")
@NotNull
private String birthDate;
@Column(name = "drivers_license_number")
@NotNull
private String driversLicenseNumber;
@Column
@NotNull
private String address;
@Column
@NotNull
private String zip;
@Column
@NotNull
private String email;
@Column(name = "home_phone")
@NotNull
private String homePhone;
@Column(name = "work_phone")
@NotNull
private String workPhone;
@Column(name = "requested_amount")
@NotNull
private String requestedAmount;
@Column
@NotNull
private String employer;
@Column(name = "active_military")
@NotNull
private String activeMilitary;
@Column(name = "income_type")
@NotNull
private String incomeType;
@Column(name = "monthly_income")
@NotNull
private String monthlyIncome;
@Column
@Temporal(TemporalType.DATE)
@NotNull
private Date date1;
@Column
@Temporal(TemporalType.DATE)
@NotNull
private Date date2;
@Column(name = "frequency")
@NotNull
private String frequency;
//getters and setters
Any help greatly appreciated. Thanks.
尝试,
@RequestMapping( method = RequestMethod.POST, consumes = {"application/x-www-form-urlencoded", "application/json"})
必须将 BindingResult 添加到我的参数中才能发现问题。这样做之后,我很明显 ModelAttribute 无法绑定来自我 post 的日期字符串并将其转换为 java.util.Date 。需要将 InitBinding 自定义转换器添加到我的控制器 class。