无法在 jpa spring 中将“java.lang.String”类型的值转换为所需类型“java.lang.Integer”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48027860/
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
Failed to convert value of type 'java.lang.String' to required type 'java.lang.Integer' In jpa spring
提问by MrSir
I have the following entity persisted through h2 in a JPA spring project
我在 JPA spring 项目中通过 h2 保留了以下实体
public class Product implements DomainObject{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Version
private Integer version;
private String description;
private BigDecimal price;
private String imageUrl;
public Product() {
}
// getters and setters
}
I have an HTML page with a form to enter new data like this
我有一个带有表单的 HTML 页面,可以输入这样的新数据
<form class="form-horizontal" th:object="${product}"
th:action="@{/product}" method="post">
<input type="hidden" th:field="*{id}"/>
<div class="form-group">
<label class="col-sm-2 control-label">Description:</label>
<div class="col-sm-10">
<input type="text" class="form-control" th:field="*{description}"/>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Price:</label>
<div class="col-sm-10">
<input type="text" class="form-control" th:field="*{price}"/>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Image Url:</label>
<div class="col-sm-10">
<input type="text" class="form-control" th:field="*{imageUrl}"/>
</div>
</div>
<div class="row">
<button type="submit" class="btn btn-default">Submit</button>
</div>
</form>
This is the method that's handling the post from the controller
这是处理来自控制器的帖子的方法
@PostMapping("/product")
public String saveOrUpdateProduct(Product product){
productService.saveOrUpdate(product);
return "redirect:/product/"+product.getId();
}
And this is the saveOrUpdate method in the autowired service class that handles interaction with the database
这是处理与数据库交互的自动装配服务类中的 saveOrUpdate 方法
private EntityManagerFactory emf;
@PersistenceUnit
public void setEmf(EntityManagerFactory emf) {
this.emf = emf;
}
@Override
public Product saveOrUpdate(Product domainObject) {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
Product savedProduct = em.merge(domainObject);
em.getTransaction().commit();
return savedProduct;
}
When I go to the HTML page with the form and I try to submit I have
当我使用表单转到 HTML 页面并尝试提交时,我有
Failed to convert value of type 'java.lang.String' to required type 'java.lang.Integer'; nested exception is java.lang.NumberFormatException: For input string: "null"
无法将“java.lang.String”类型的值转换为所需的“java.lang.Integer”类型;嵌套异常是 java.lang.NumberFormatException: For input string: "null"
回答by Ele
Probably you have to annotate the param product in your controller:
可能您必须在控制器中注释参数产品:
@PostMapping("/product")
public String saveOrUpdateProduct(@RequestBody Product product)
Spring Javadoc for @RequestBody
@RequestBody 的 Spring Javadoc
@Target(value=PARAMETER)
@Retention(value=RUNTIME)
@Documented
public @interface RequestBody
/*Annotation indicating a method parameter should be bound to the body of the
web request. The body of the request is passed through an
HttpMessageConverter to resolve the method argument depending on the
content type of the request.*/
Hope this helps!
希望这可以帮助!
回答by MrSir
The problem had to do with
问题与
@PostMapping("/product")
public String saveOrUpdateProduct(Product product){
productService.saveOrUpdate(product);
return "redirect:/product/"+product.getId();
}
I used to call product.getId()
from the original un-merged product and it didn't have an Id yet, i solved it returning a saved Product
我曾经product.getId()
从原始未合并的产品中调用它,但它还没有 Id,我解决了它返回一个保存的产品
@PostMapping("/product")
public String saveOrUpdateProduct(Product product){
Product savedProduct = productService.saveOrUpdate(product);
return "redirect:/product/"+savedProduct.getId();
}
回答by Shahid Hussain Abbasi
Use @JsonIgnoreProperties(ignoreUnknown = true) with your DTO
将 @JsonIgnoreProperties(ignoreUnknown = true) 与您的 DTO 一起使用