Skip to content
Advertisement

Spring server POST method gives JSON error, Invalid UTF-8 middle byte

I have a JavaFX app and a Java server, I am trying to send a post method with a JSON but when I do I get this error from the server

Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Invalid UTF-8 middle byte 0x77; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Invalid UTF-8 middle byte 0x77
 at [Source: (PushbackInputStream); line: 1, column: 209] (through reference chain: java.util.ArrayList[0]->pwsztar.edu.pl.project_kino.dto.Film["opis"])]

My post request looks like this

String json = new Gson().toJson(filmsToDelete);
            System.out.println(json);

            var request = new HttpPost("http://localhost:8080/api/v1/removeFilm");
            request.setHeader("Content-type", "application/json;charset=UTF-8");
            request.setEntity(new StringEntity(json));

            HttpResponse response = client.execute(request);
            System.out.println(response.getStatusLine());

The filmToDelete is and arraylist of Film objects. And this is mine controller methond on the server

@PostMapping(path = "removeFilm")
    public void removeFilms(@RequestBody ArrayList<Film> filmsToRemove){
        filmService.printFilms(filmsToRemove);
    }

So what I would like to know is where is the problem, and maybe how can I fix it. If any additional info let me know

Advertisement

Answer

Set the correct encoding on the StringEntity:

request.setEntity(new StringEntity(json, "UTF-8"));
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement