【问题标题】:Spring REST 400 Bad Request with x-www-urlformencoded POST of Body to @ModelAttribute Domain ObjectSpring REST 400 Bad Request with x-www-urlformencoded POST of Body to @ModelAttribute Domain Object
【发布时间】:2017-04-23 05:00:30
【问题描述】:

嗨,我伸出手是希望我忽略了一些微不足道的事情。我认为此处的 url 编码帖子没有​​任何问题。尽管我试图抓住它,但我无法获得 400 Bad Request 异常消息的详细信息。它可能是我在处理程序中没有映射到的类。

无论如何,这适用于普通的 application/json 帖子,但在通过 url 编码发布时存在问题。我什至没有到达我的控制器,因此当涉及 url 编码时,我相当确定这是对我的示例实体的绑定问题。当我使用 @RequestBody 并使用 application/json 发布时,它绑定得很好,但在通过 urlencoding 发布时 @ModelAttribute 出现问题。对于 urlencoding 帖子,我的身体有什么问题?

批量编辑文本

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

用于 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(同样,这适用于应用程序/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

非常感谢任何帮助。谢谢。

【问题讨论】:

    标签: spring rest exception postman http-status-code-400


    【解决方案1】:

    试试,

    @RequestMapping( method = RequestMethod.POST, consumes = {"application/x-www-form-urlencoded", "application/json"})
    

    【讨论】:

    • 感谢您的回复。这没有用。当我用 MultiValueMap 替换 RequestBody 的 ModelAttribute 时,它​​适用于 x-www-urlencoded 正文帖子。不知道为什么这个特定的帖子不能与 ModelAttribute 一起使用。我的许多其他帖子都没有其他问题 ModelAttribute 。这是关于这篇文章中 ModelAttribute 无法处理的特定数据吗?
    • 终于弄清楚了。 public Lead save(@Valid @ModelAttribute final Lead entity, BindingResult bindingResult)
    【解决方案2】:

    必须将 BindingResult 添加到我的参数中才能发现问题。这样做之后,对我来说很明显 ModelAttribute 无法从我的帖子中绑定日期字符串并将其转换为 java.util.Date 。需要将 InitBinding 自定义转换器添加到我的控制器类。

    【讨论】:

    • @InitBinder public void initBinder(WebDataBinder webDataBinder) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); webDataBinder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true)); }
    猜你喜欢
    • 2017-09-26
    • 1970-01-01
    • 1970-01-01
    • 2013-08-21
    • 1970-01-01
    • 1970-01-01
    • 2017-05-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多