【问题标题】:Failed to convert value of type 'java.lang.String' to required type 'java.lang.Integer' In jpa spring无法在 jpa spring 中将类型“java.lang.String”的值转换为所需类型“java.lang.Integer”
【发布时间】:2018-06-10 04:54:54
【问题描述】:

我在 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>

这是从控制器处理帖子的方法

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

这是处理与数据库交互的自动装配服务类中的 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: For input string: "null"

【问题讨论】:

  • 你说这是为了新数据,对吗?如果是这样,为什么你有 id 的隐藏输入?
  • @Powerlord 因为他想要保存或更新
  • 这个输入到底得到了什么:&lt;input type="hidden" th:field="*{id}"/&gt;。我怀疑id 从控制器传递到您的模板时将为空。
  • 想知道这个异常来自哪里?也许发布 EXCEPTION PLUS STACK TRACE 会有所帮助,否则您希望人们猜测。我不是在猜测。祝你好运

标签: java spring spring-mvc jpa


【解决方案1】:

可能您必须在控制器中注释参数产品:

@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.*/

希望这会有所帮助!

【讨论】:

  • 如果我添加该注释,则会出现此错误 出现意外错误(类型=不支持的媒体类型,状态=415)。不支持内容类型“application/x-www-form-urlencoded;charset=UTF-8”
  • 尝试在表单中设置字符集&lt;form action="..." accept-charset="ISO-8859-1"&gt;
【解决方案2】:

问题与

有关
@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();
}

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-22
      • 2017-04-24
      • 2017-04-21
      • 2021-03-13
      • 2014-02-01
      • 2017-06-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多