spring-boot get a "400 bad request" error, the detail is followed

Viewed 27904

I am very new on spring boot. So the question I asked maybe cause by I missing something. I use spring boot 1.3 and freemarker to build a web site. when I submit a form( which some properties I don't fill in and this situation is I need) I get a "400 bad request". However, when I fill in all the properties of the form and submit, it works. So, what's the reason and How can I fix it?

The form is the following code.

    <label for="steelName" >品名:</label>
    <input type="text" id="steelName" name="steelName" placeholder="品名" value="${steelName!}" required><br/>

    <label for="steelCode" >钢种:</label>
    <input type="text" id="steelCode" name="steelCode" placeholder="钢种" value="${steelCode!}" required><br/>

    <label for="basic_price" >基价:</label>
    <input type="text" id="basicPrice" name="basicPrice" placeholder="2000" value="${basicPrice!}" required>
    <label>元</label><br/>

    <label for="steel_factory" >钢厂:</label>
    <input type="text" id="steelFactory" name="steelFactory" placeholder="钢厂名称" value="${steelFactory!}" >

    <label for="city" >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;城市:</label>
    <input type="text" id="city" name="city" placeholder="城市" value="${city!}" >

    <br/>

    <label for="width" >宽度范围:</label>
    <input type="text" id="lowWidth" name="lowWidth" placeholder="1.0" value="${lowWidth!}" >
    <label for=""> - </label>
    <input type="text" id="highWidth" name="highWidth" placeholder="1.6" value="${highWidth!}" >
    <label for=""> m </label>

    <label for="width_step">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;宽度步长:</label>
    <input type="text" id="widthStep" name="widthStep" placeholder="1.0" value="${widthStep!}" >
    <label for=""> m </label>

    <label for="width_price">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;宽度差价:</label>
    <input type="text" id="widthPrice" name="widthPrice" placeholder="1.0" value="${widthPrice!}" >
    <label for=""> 元 </label>

    </br>

    <label for="thickness" >厚度范围:</label>
    <input type="text" id="lowThickness" name="lowThickness" placeholder="1.0" value="${lowThickness!}" >
    <label for=""> - </label>
    <input type="text" id="highThickness" name="highThickness" placeholder="1.6" value="${highThickness!}" >
    <label for=""> cm </label>

    <label for="thickness_step">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;厚度步长:</label>
    <input type="text" id="thicknessStep" name="thicknessStep" placeholder="1.0" value="${thicknessStep!}" >
    <label for=""> cm </label>

    <label for="thickness_price">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;厚度差价:</label>
    <input type="text" id="thicknessPrice" name="thicknessPrice" placeholder="1.0" value="${thicknessPrice!}" >
    <label for=""> 元 </label>

    </br>

    <button type="submit">提交</button>

</form>

The Controller.java

    @RequestMapping(value = "/admin/add/steel", method = RequestMethod.POST)
    public String addSteel(@ModelAttribute("steel")Steel steel, Model model) {

    log.info("add "+steel.getSteelName()+" info.");
    //model.addAttribute(FormAuthenticationFilter.DEFAULT_USERNAME_PARAM, userName);
    steel = steelService.save(steel);
    model.addAttribute("steel", steel);
    return "steel/detail";
}

The steel entity

 @Entity
        @Table(name = "t_steel")
        public class Steel extends IdEntity {    
        @Column(name = "steel_name", unique = true)
        private String steelName;
        @Column(name = "steel_code", unique = true)
        private String steelCode;
        @Column(name = "basic_price", unique = true)
        private int basicPrice;

    @Column(name = "steel_factory", nullable = true)
    private String steelFactory = null;

    @Column(name = "city", nullable = true)
    private String city = null;

    @Column(name = "low_width", nullable = true)
    private Double lowWidth = null;

    @Column(name = "high_width", nullable = true)
    private Double highWidth  = null;

    @Column(name = "width_step", nullable = true)
    private Double widthStep  = null;

    @Column(name = "width_price", nullable = true)
    private Integer widthPrice  = null;

The question:

This is the link to form This is the link toerror

2 Answers

Add below method in controller.java. it will give more detail error report in addition to 400 bad request. here apiError is my customized exception. you can give any user defined exception here.

@ResponseStatus(value = HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = {MissingServletRequestParameterException.class})
    ApiError handleMethodArgumentNotValid(MissingServletRequestParameterException ex) {

        return new ApiError(ErrorCode.MISSING_REQUIRED_PARAMS, ex.getMessage());
    }
Related