简体   繁体   English

Spring Boot com.fasterxml.jackson.core.JsonParseException:无法识别的令牌

[英]Spring Boot com.fasterxml.jackson.core.JsonParseException: Unrecognized token

I work with a Spring Boot project and I would like to perform multiple POST request after the program is started than to use the cURL afterward manually. 我使用的是Spring Boot项目,我想在程序启动后执行多个POST请求,而不是事后手动使用cURL。 The purpose will be to store a few data in the storage and make the platform ready for further operations. 目的是在存储器中存储一些数据,并使平台为进一步操作做好准备。 The original cURL command I use (which worked fine), 我使用的原始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 

The API that takes it, 需要它的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);
    }

Now I make it right after the Spring boot is loaded, 现在,我在加载了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();
        }
    }
}

I get the following error provided, 我收到以下错误消息:

{"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"}

How do I set the media data properly in this scenario for the POST call? 在这种情况下,如何为POST调用正确设置媒体数据?

Following code adds values as request parameters. 以下代码将值添加为请求参数。

    post.setEntity(new UrlEncodedFormEntity(arguments));

Considering your API expects JSON content in request body, you need to use StringEntity instead of UrlEncodedFormEntity 考虑到您的API在请求正文中需要JSON内容,您需要使用StringEntity而不是UrlEncodedFormEntity

    post.setEntity(new StringEntity("{ \"name_of_doctor\": \"\", \"price\": \"12.5\" }", Charset.forName("UTF-8")));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Boot com.fasterxml.jackson.core.JsonParseException:无法识别的令牌“食谱” - Spring Boot com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'Recipe' 错误 com.fasterxml.jackson.core.JsonParseException:无法识别的令牌 - Error com.fasterxml.jackson.core.JsonParseException: Unrecognized token 由以下原因引起的错误:com.fasterxml.jackson.core.JsonParseException:无法识别的令牌“ Employee”:正在期待(“ true”,“ false”或“ null”) - Error Caused by: com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'Employee': was expecting ('true', 'false' or 'null') com.fasterxml.jackson.core.JsonParseException:无法识别的令牌'Hello':期待(JSON字符串,数字,数组,) - com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'Hello': was expecting (JSON String, Number, Array,) Apache Camel when deserializing an object, it throws an exception: com.fasterxml.jackson.core.JsonParseException: Unrecognized token - Apache Camel when deserializing an object, it throws an exception: com.fasterxml.jackson.core.JsonParseException: Unrecognized token 引起:com.fasterxml.jackson.core.JsonParseException:无法识别的令牌&#39;okhttp3&#39;:期待(JSON字符串&#39;) - Caused by: com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'okhttp3': was expecting (JSON String') com.fasterxml.jackson.core.JsonParseException:无法识别的字符转义符&#39;U&#39;(代码85) - com.fasterxml.jackson.core.JsonParseException: Unrecognized character escape 'U' (code 85) 如何使无法识别的字符转义 &#39;.&#39; (代码 46)被识别 - com.fasterxml.jackson.core.JsonParseException - How to make Unrecognized character escape '.' (code 46) to be Recognized - com.fasterxml.jackson.core.JsonParseException com.fasterxml.jackson.core.JsonParseException:意外的字符 - com.fasterxml.jackson.core.JsonParseException: Unexpected character com.fasterxml.jackson.core.JsonParseException是否为* .json.swp? - com.fasterxml.jackson.core.JsonParseException for *.json.swp?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM