【问题标题】:spring insert a row into a mysql table using spring JDBCTemplatespring 使用 spring JDBCTemplate 在 mysql 表中插入一行
【发布时间】:2020-03-20 17:37:23
【问题描述】:

我正在尝试从 Spring 应用程序在“ride”表中创建一个新行,我收到以下错误

{
org.springframework.web.client.HttpServerErrorException: 500 null

    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:94)
    at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:700)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:653)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:613)
    at org.springframework.web.client.RestTemplate.put(RestTemplate.java:432)
    at com.pluralsight.controller.RestControllerTest.testCreateRide(RestControllerTest.java:24)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:298)
    at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:292)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at java.base/java.lang.Thread.run(Thread.java:834)
}

我使用rest模板将信息添加到mysql表中,如下所示:

    RestTemplate restTemplate = new RestTemplate();

    Ride ride = new Ride();
    ride.setName("Bobsled Trail");
    ride.setDuration(33);

    restTemplate.put("http://localhost:8080/ride_tracker/ride", ride);

    System.out.println("Ride: " + ride);

项目中的所有代码都可以在以下链接中找到: https://github.com/roscasend/ride_tracker

最好的问候, 奥勒良

【问题讨论】:

  • 检查你的类'RideRepositoryImpl.java'第43行

标签: mysql spring spring-jdbc jdbctemplate


【解决方案1】:

首先,作为一个标准,我们应该使用 POST 来插入和 PUT 来进行更新操作。当服务器端出现问题时会发生此错误。 (HTTP 500 状态 - 内部服务器错误)

通过查看客户端很难说会发生什么。所以请发布服务器错误堆栈跟踪。

还有一件事注意到在你的查询中给出参数的对象数组 新对象[] {名,姓}

jdbcTemplate.update("insert into ride (name, duration) values (?,?)", new Object[] { ride.getName(), ride.getDuration()}); 

【讨论】:

  • 如何查看服务器错误堆栈跟踪?我按照你说的更新了代码,但错误仍然存​​在。明天我会用 post 方法再次检查。
  • 你已经更新了代码->构建->部署我猜,这个代码部署在哪里,查看那个服务器日志。如果它的 tomcat catalin.log 等。
  • 不是tomcat/log文件夹中显示的任何日志文件
  • 当您的服务器启动时,您是否在 catalina.out 文件中看到日志?还要让我知道表的主键是什么。在您签入的代码中,我看到主键部分已被注释掉。
【解决方案2】:

根据您的代码,您的存储库返回 null 对象,服务器尝试将 null 转换为 @ResponseBody。尝试在 RideController.java 中的以下方法中处理代码中的 null。或使用@ResponseBodyNull 代替@ResponseBody。

@RequestMapping(value = "/ride",  method = RequestMethod.PUT)
public @ResponseBody Ride createRide(@RequestBody Ride ride) {
    return rideService.createRide(ride);
}

【讨论】:

    猜你喜欢
    • 2017-07-24
    • 2010-10-26
    • 2012-03-10
    • 1970-01-01
    • 2017-01-27
    • 1970-01-01
    • 2013-07-25
    • 1970-01-01
    • 2017-07-30
    相关资源
    最近更新 更多