java spring-boot 出现“400 bad request”错误,详情如下

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36028046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 00:52:58  来源:igfitidea点击:

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

javaformsspring-mvcspring-boothttp-headers

提问by user2688702

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?

我对弹簧靴很陌生。所以我问的问题可能是因为我遗漏了一些东西。我使用 spring boot 1.3 和 freemarker 来构建一个网站。当我提交表单(我没有填写某些属性,而这种情况是我需要的)时,我收到“400 个错误的请求”。但是,当我填写表单的所有属性并提交时,它起作用了。那么,是什么原因,我该如何解决?

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

控制器.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 formThis is the link toerror

这是表格的链接 这是错误的链接

回答by Daniel Lavoie

Add the following property to your spring boot configuration logging.level.org.springframework.web=DEBUG. With such log level, you will have detailled error explaining the cause of the 400.

将以下属性添加到您的 Spring Boot 配置中logging.level.org.springframework.web=DEBUG。使用这样的日志级别,您将获得解释 400 原因的详细错误。

You could also drop a logback.xml in in your classpath (src/main/resources for exemple) and add this appender :

你也可以在你的类路径(例如 src/main/resources)中放入一个 logback.xml 并添加这个 appender:

<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <logger name="org.springframework.web" level="DEBUG" />

    <root level="info">
        <appender-ref ref="STDOUT" />
    </root>
</configuration>

Good luck.

祝你好运。