Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法转换类型为';java.lang.String';至所需类型';java.lang.Integer';在jpa春天_Java_Spring_Spring Mvc_Jpa - Fatal编程技术网

无法转换类型为';java.lang.String';至所需类型';java.lang.Integer';在jpa春天

无法转换类型为';java.lang.String';至所需类型';java.lang.Integer';在jpa春天,java,spring,spring-mvc,jpa,Java,Spring,Spring Mvc,Jpa,我在一个JPA spring项目中通过h2保存了以下实体 public class Product implements DomainObject{ @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; @Version private Integer version; private String description; private BigDe

我在一个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
}
我有一个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>
这是autowired服务类中处理与数据库交互的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;
}
当我带着表单进入HTML页面并尝试提交时,我有

无法将“java.lang.String”类型的值转换为所需的“java.lang.Integer”类型; 嵌套异常为java.lang.NumberFormatException:对于输入字符串:“null”


您可能需要在控制器中为param产品添加注释:

@PostMapping("/product")
public String saveOrUpdateProduct(@RequestBody Product product) 
@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.*/

希望这有帮助这个问题与

@PostMapping("/product")
public String saveOrUpdateProduct(Product product){
    productService.saveOrUpdate(product);
    return "redirect:/product/"+product.getId();
}
我曾经从原始的未合并产品调用
product.getId()
,但它还没有Id,我解决了这个问题,返回了一个保存的产品

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

在DTO中使用@JsonIgnoreProperties(ignoreUnknown=true)

您说过这是用于新数据的,对吗?如果是这样,为什么您要为
id
?@Powerlord隐藏输入,因为他想要保存或更新此输入的确切内容:
。我怀疑当从控制器传递到模板时,
id
将为空。不知道这个异常是从哪里来的?也许发布异常加堆栈跟踪会有所帮助,否则你会让人们猜测。我不是在猜测。好运气如果我添加了注释,我有此错误,则出现了意外错误(类型=不支持的媒体类型,状态=415)。“内容类型”应用程序/x-www-form-urlencoded;charset=UTF-8'不支持尝试在表单中设置字符集
@PostMapping("/product")
public String saveOrUpdateProduct(Product product){
    Product savedProduct = productService.saveOrUpdate(product);
    return "redirect:/product/"+savedProduct.getId();
}