简体   繁体   中英

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. 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 -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,

@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,

@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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM