添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
千杯不醉的绿茶  ·  CMakeSettings.json ...·  1 年前    · 
虚心的灯泡  ·  Oops!!! - 简书·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Failed to convert value of type 'java.lang.String' to required type 'java.lang.Integer' In jpa spring

Ask Question

I have the following entity persisted through h2 in a JPA spring project

public class Product implements DomainObject{
    @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

<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 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 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 class="row">
            <button type="submit" class="btn btn-default">Submit</button>
    </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

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

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"

You said this is for new data, correct? If so, why do you have a hidden input for the id? – Powerlord Dec 29, 2017 at 20:11 What exactly does get put for this input: <input type="hidden" th:field="*{id}"/>. I suspect that id will be null when passed from controller to your template. – tsolakp Dec 29, 2017 at 20:37 Wonder where this exception comes from? maybe posting the EXCEPTION PLUS STACK TRACE would help, otherwise you want people to guess. I'm not guessing. Good luck – user8558216 Dec 30, 2017 at 6:52

Probably you have to annotate the param product in your controller:

@PostMapping("/product")
public String saveOrUpdateProduct(@RequestBody Product product) 

Spring Javadoc for @RequestBody

@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!

If i add that annotation i have this error There was an unexpected error (type=Unsupported Media Type, status=415). Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported – MrSir Dec 29, 2017 at 20:16
@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

@PostMapping("/product")
public String saveOrUpdateProduct(Product product){
    Product savedProduct = productService.saveOrUpdate(product);
    return "redirect:/product/"+savedProduct.getId();
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.