我正在尝试使用SPRING Converter
接口将id转换为对象但是当我发布表单时我遇到了400错误的请求错误我有一个看起来像这样的代码
这是转换器
@Component
public class LocationConverter implements Converter<Object, Location> {
@Autowired
LocationService locationService;
@Override
public Location convert(Object element) {
Integer id = Integer.parseInt((String)element);
Location l = locationService.getById(id);
return l;
}
}
我正在使用基于注释的配置,所以我正在注册它
@Override
public void addFormatters(FormatterRegistry registry){
registry.addConverter(new CategoryConverter());
registry.addConverter(new LocationConverter());
registry.addConverter(new SupplierConverter());
}
控制器
@RequestMapping(value="/create", method=RequestMethod.POST)
public String createOrUpdateArticle(@ModelAttribute Article article,String action,MultipartFile file)
throws IOException{
if(action.equals("create")){
if( !file.isEmpty() ) {
article.setImage(file.getBytes());
}
articleService.create(article);
}
if(action.equals("update")){
articleService.update(article);
}
return "redirect:/article/";
}
并在视图中
<f:form modelAttribute="article" enctype="multipart/form-data" method="post"
action="${pageContext.request.contextPath}/article/create">
<input type="hidden" name="action" value="${action}"/>
<f:input type="hidden" path="id"/>
<div class="col-md-6 form-group">
<label>CodeBarre</label>
<f:input path="code" class="form-control"/>
</div>
<div class="col-md-6 form-group">
<label>Description</label>
<f:input path="description" class="form-control"/>
</div>
<div class="col-md-6 form-group">
<label>Prix Grossier</label>
<f:input path="price1" class="form-control"/>
</div>
<div class="col-md-6 form-group">
<label>Prix Semi Grossier</label>
<f:input path="price2" class="form-control"/>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Categorie</label>
<f:select cssClass="form-control" path="category" items="${categories}"
itemValue="id" itemLabel="name"/>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Location</label>
<f:select cssClass="form-control" path="location" items="${locations}"
itemValue="id" itemLabel="name"/>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Fournisseur</label>
<f:select cssClass="form-control" path="suppliers" items="${suppliers}"
multiple="true" itemValue="id" itemLabel="firstName"/>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Photo</label>
<input type="file" name="file" class="form-control" />
</div>
</div>
<button type="submit" class="btn btn-sm-default pull-right">Ajouter</button>
</f:form>
错误消息
Field error in object 'article' on field 'category': rejected value [3]; codes [typeMismatch.article.category,typeMismatch.category,typeMismatch.ma.stock.app.entities.Category,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [article.category,category]; arguments []; default message [category]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'ma.stock.app.entities.Category' for property 'category'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type @javax.persistence.ManyToOne ma.stock.app.entities.Category for value '3'; nested exception is java.lang.NullPointerException]
字段'location'上对象'article'中的字段错误:被拒绝的值[2];代码[typeMismatch.article.location,typeMismatch.location,typeMismatch.ma.stock.app.entities.Location,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable:codes [article.location,location];参数[];默认消息[location]];默认消息[无法将类型'java.lang.String'的属性值转换为属性'location'所需的类型'ma.stock.app.entities.Location';嵌套异常是org.springframework.core.convert.ConversionFailedException:无法从类型java.lang.String转换为类型@javax.persistence.OneToOne ma.stock.app.entities.Location为值'2';嵌套异常是java.lang.NullPointerException] 字段“供应商”中对象“文章”中的字段错误:被拒绝的值[4];代码[typeMismatch.article.suppliers,typeMismatch.suppliers,typeMismatch.java.util.Set,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable:codes [article.suppliers,suppliers];参数[];默认消息[供应商]];默认消息[无法将类型'java.lang.String'的属性值转换为属性'供应商'所需的类型'java.util.Set';嵌套异常是org.springframework.core.convert.ConversionFailedException:无法从类型java.lang.String转换为类型@javax.persistence.ManyToMany @ javax.persistence.JoinTable ma.stock.app.entities.Supplier for value'4 “;嵌套异常是java.lang.NullPointerException]
我不知道我在这里缺少什么, 提前谢谢。