java - Spring Boot com.fasterxml.jackson.core.JsonParseException:无法识别的令牌
问题描述
我使用 Spring Boot 项目,我想POST
在程序启动后执行多个请求,而不是之后手动使用 cURL。目的是在存储中存储一些数据,并使平台为进一步操作做好准备。我使用的原始cURL
命令(效果很好),
$ curl -i -X POST -H "Content-Type:application/json" -d "{\"name_of_doctor\" : \"Monika2\", \"price\": \"12.5\"}" http://localhost:8080/api/v1/appointments/createAppointment
接受它的 API,
@PostMapping(value = "/createAppointment", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
public ResponseEntity<Appointment> create(@RequestBody Appointment appointment) {
java.sql.Date date = new java.sql.Date(Calendar.getInstance().getTime().getTime());
java.sql.Time time = new java.sql.Time(Calendar.getInstance().getTime().getTime());
appointment.setAppointment_date(date);
appointment.setCraeted_at(time);
// we create the appointment, because, the doctor is available
appointment.setStatus(new Status(true));
// appointment.setStatus(true);
service.save(appointment);
return ResponseEntity.status(HttpStatus.CREATED).body(appointment);
}
现在我在加载 Spring boot 后立即制作它,
@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");
List<NameValuePair> arguments = new ArrayList<>(2);
arguments.add(new BasicNameValuePair("name_of_doctor", "Monika"));
arguments.add(new BasicNameValuePair("price", "12.5"));
try {
post.setEntity(new UrlEncodedFormEntity(arguments));
//post.addHeader("content-type", "application/x-www-form-urlencoded");
post.setHeader("Accept", "application/json");
post.setHeader("Content-type", "application/json");
HttpResponse response = client.execute(post);
System.out.println(EntityUtils.toString(response.getEntity()));
} catch (IOException e) {
e.printStackTrace();
}
}
}
我收到以下错误,
{"timestamp":"2019-02-09T06:45:36.393+0000","status":400,"error":"Bad Request","message":"JSON parse error: Unrecognized token 'name_of_doctor': was expecting 'null', 'true', 'false' or NaN; nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'name_of_doctor': was expecting 'null', 'true', 'false' or NaN\n at [Source: (PushbackInputStream); line: 1, column: 16]","path":"/api/v1/appointments/createAppointment"}
在这种情况下,如何为 POST 调用正确设置媒体数据?
解决方案
以下代码将值添加为请求参数。
post.setEntity(new UrlEncodedFormEntity(arguments));
考虑到您的 API 需要JSON
请求正文中的内容,您需要使用StringEntity
而不是UrlEncodedFormEntity
post.setEntity(new StringEntity("{ \"name_of_doctor\": \"\", \"price\": \"12.5\" }", Charset.forName("UTF-8")));
推荐阅读
- android - onActivityResult 未调用。既不是父片段也不是嵌套片段
- php - 我可以将 Python2 添加到 AWS Elastic Beanstalk PHP 平台吗?
- python - saspy sas内核在jupyter中不可见
- java - 无法转换为 json
- javascript - 如何使 'var data = data2[i]' 成为一个对象?
- vue.js - Vue:相同的路由相同的组件,但不同的参数
- php - Sqlite 在布尔值上调用成员函数 bindParam()
- html - Bower/Grunt 开始使用 [Wrap]Bootstrap 主题
- python - 将每个项目与一行中的最后一个非空值配对
- javascript - 关闭特定模式后重新加载页面