我是个新手,穿着弹簧靴。所以我问的问题可能是因为我漏掉了什么。我使用spring boot 1.3和freemarker来构建一个网站。当我提交一个表单(一些我没有填写的属性,这种情况是我需要的),我得到一个"400错误的请求“。但是,当我填写表单的所有属性并提交时,它就可以工作了。那么,原因是什么,我如何才能修复它?
表单是下面的代码。
<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" > 城市:</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"> 宽度步长:</label>
<input type="text" id="widthStep" name="widthStep" placeholder="1.0" value="${widthStep!}" >
<label for=""> m </label>
<label for="width_price"> 宽度差价:</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"> 厚度步长:</label>
<input type="text" id="thicknessStep" name="thicknessStep" placeholder="1.0" value="${thicknessStep!}" >
<label for=""> cm </label>
<label for="thickness_price"> 厚度差价:</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";
}钢实体
@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的链接
发布于 2016-03-16 16:21:55
将以下属性添加到spring boot配置logging.level.org.springframework.web=DEBUG中。有了这样的日志级别,您将有详细的错误来解释400的原因。
您还可以在类路径中放置一个资源(例如,src/ logback.xml /logback.xml),并添加以下附加器:
<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>祝好运。
发布于 2021-03-12 16:15:47
在controller.java中添加以下方法。除了400个错误请求之外,它还将提供更详细的错误报告。这里的apiError是我定制的例外。您可以在此处提供任何用户定义的异常。
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
@ExceptionHandler(value = {MissingServletRequestParameterException.class})
ApiError handleMethodArgumentNotValid(MissingServletRequestParameterException ex) {
return new ApiError(ErrorCode.MISSING_REQUIRED_PARAMS, ex.getMessage());
}https://stackoverflow.com/questions/36028046
复制相似问题