【问题标题】:Spring server POST method gives JSON error, Invalid UTF-8 middle byteSpring 服务器 POST 方法给出 JSON 错误,无效的 UTF-8 中间字节
【发布时间】:2022-02-03 07:53:31
【问题描述】:

我有一个 JavaFX 应用程序和一个 Java 服务器,我正在尝试使用 JSON 发送一个 post 方法,但是当我这样做时,我从服务器收到此错误

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"])]

我的发帖请求如下所示

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());

filmToDelete 是 Film 对象的数组列表。 这是服务器上的我的控制器方法

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

所以我想知道问题出在哪里,也许我该如何解决。如果有任何其他信息,请告诉我

【问题讨论】:

    标签: java spring


    【解决方案1】:

    在StringEntity上设置正确的编码:

    request.setEntity(new StringEntity(json, "UTF-8"));
    

    【讨论】:

    • 好的,有帮助,谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-03
    相关资源
    最近更新 更多