Spring Boot启动后自动发起POST请求报JsonParseException问题
问题分析与解决方案
你碰到的400 Bad Request错误本质是请求数据格式不匹配——你的后端API通过@RequestBody声明要接收JSON格式的数据,但你用UrlEncodedFormEntity发送的是表单编码(application/x-www-form-urlencoded)的参数,这直接导致Jackson解析JSON时失败,抛出了"Unrecognized token 'name_of_doctor'"的错误。
下面给你两种正确的实现方式,以及一个优化建议:
方式一:手动构造JSON请求体
直接拼接符合要求的JSON字符串,用StringEntity封装并设置正确的Content-Type头:
@SpringBootApplication @EnableTransactionManagement public class AppointmentApplication { public static void main(String[] args) { System.out.println("\n\nAppointment Manager\n\n"); SpringApplication.run(AppointmentApplication.class, args); HttpClient client = HttpClientBuilder.create().build(); HttpPost post = new HttpPost("http://localhost:8080/api/v1/appointments/createAppointment"); // 构造JSON格式的请求体,注意转义引号或者用双引号包裹字符串 String jsonRequestBody = "{\"name_of_doctor\": \"Monika\", \"price\": \"12.5\"}"; try { // 使用StringEntity封装JSON,指定Content-Type为application/json StringEntity requestEntity = new StringEntity( jsonRequestBody, ContentType.APPLICATION_JSON ); post.setEntity(requestEntity); HttpResponse response = client.execute(post); System.out.println("请求响应结果:" + EntityUtils.toString(response.getEntity())); } catch (IOException e) { e.printStackTrace(); } } }
方式二:用Jackson序列化对象(更优雅)
如果你的Appointment实体类已经定义好,推荐用Jackson把对象直接序列化为JSON,避免手动拼接字符串出错:
@SpringBootApplication @EnableTransactionManagement public class AppointmentApplication { public static void main(String[] args) { System.out.println("\n\nAppointment Manager\n\n"); SpringApplication.run(AppointmentApplication.class, args); HttpClient client = HttpClientBuilder.create().build(); HttpPost post = new HttpPost("http://localhost:8080/api/v1/appointments/createAppointment"); // 实例化Appointment对象并设置属性 Appointment appointment = new Appointment(); appointment.setName_of_doctor("Monika"); appointment.setPrice("12.5"); // 用Jackson的ObjectMapper序列化对象为JSON ObjectMapper objectMapper = new ObjectMapper(); try { String jsonRequestBody = objectMapper.writeValueAsString(appointment); StringEntity requestEntity = new StringEntity( jsonRequestBody, ContentType.APPLICATION_JSON ); post.setEntity(requestEntity); HttpResponse response = client.execute(post); System.out.println("请求响应结果:" + EntityUtils.toString(response.getEntity())); } catch (IOException e) { e.printStackTrace(); } } }
额外优化:用ApplicationRunner确保应用就绪后再请求
你当前在main方法里直接发送请求,可能会碰到Spring Boot刚启动、容器还没完全就绪的情况,导致连接失败。推荐用Spring提供的ApplicationRunner来执行初始化逻辑,它会在应用上下文完全加载后自动执行:
@SpringBootApplication @EnableTransactionManagement public class AppointmentApplication { public static void main(String[] args) { System.out.println("\n\nAppointment Manager\n\n"); SpringApplication.run(AppointmentApplication.class, args); } @Bean public ApplicationRunner initAppointmentData() { return args -> { HttpClient client = HttpClientBuilder.create().build(); HttpPost post = new HttpPost("http://localhost:8080/api/v1/appointments/createAppointment"); String jsonRequestBody = "{\"name_of_doctor\": \"Monika\", \"price\": \"12.5\"}"; StringEntity requestEntity = new StringEntity(jsonRequestBody, ContentType.APPLICATION_JSON); post.setEntity(requestEntity); HttpResponse response = client.execute(post); System.out.println("初始化预约数据响应:" + EntityUtils.toString(response.getEntity())); }; } }
最后提个小细节:你的后端代码里有个拼写错误Craeted_at,应该是Created_at,虽然这不是当前错误的原因,但后续可能会导致数据映射问题,建议修正~
内容的提问来源于stack exchange,提问作者Arefe
相关产品推荐
相关产品推荐

