首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >spring-boot得到一个"400坏请求“错误,详细信息如下

spring-boot得到一个"400坏请求“错误,详细信息如下
EN

Stack Overflow用户
提问于 2016-03-16 14:14:54
回答 2查看 24.4K关注 0票数 4

我是个新手,穿着弹簧靴。所以我问的问题可能是因为我漏掉了什么。我使用spring boot 1.3和freemarker来构建一个网站。当我提交一个表单(一些我没有填写的属性,这种情况是我需要的),我得到一个"400错误的请求“。但是,当我填写表单的所有属性并提交时,它就可以工作了。那么,原因是什么,我如何才能修复它?

表单是下面的代码。

代码语言:javascript
运行
复制
    <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

代码语言:javascript
运行
复制
    @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";
}

钢实体

代码语言:javascript
运行
复制
 @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;

问题是:

这是指向form的链接这是指向error的链接

EN

回答 2

Stack Overflow用户

发布于 2016-03-16 16:21:55

将以下属性添加到spring boot配置logging.level.org.springframework.web=DEBUG中。有了这样的日志级别,您将有详细的错误来解释400的原因。

您还可以在类路径中放置一个资源(例如,src/ logback.xml /logback.xml),并添加以下附加器:

代码语言:javascript
运行
复制
<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>

祝好运。

票数 29
EN

Stack Overflow用户

发布于 2021-03-12 16:15:47

在controller.java中添加以下方法。除了400个错误请求之外,它还将提供更详细的错误报告。这里的apiError是我定制的例外。您可以在此处提供任何用户定义的异常。

代码语言:javascript
运行
复制
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = {MissingServletRequestParameterException.class})
    ApiError handleMethodArgumentNotValid(MissingServletRequestParameterException ex) {

        return new ApiError(ErrorCode.MISSING_REQUIRED_PARAMS, ex.getMessage());
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36028046

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档