java - Spring服务器POST方法给出JSON错误,无效的UTF-8中间字节

标签 java spring

我有一个 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());

filmToDeleteFilm 对象的数组列表。 这是服务器上我的 Controller 方法

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

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

最佳答案

StringEntity上设置正确的编码:

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

关于java - Spring服务器POST方法给出JSON错误,无效的UTF-8中间字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70967582/

相关文章:

java - maven-vault-plugin->configuration->embeddeds->embedded 中过滤器标签的含义?

java - Spring Neo4j 保存 MultipartFile

java - 这种确定 gem 棋获胜者的方法不起作用

java - Spring Rest-API - 403 禁止错误响应

Java:通过Spring创建基于OCP的工厂?

java - 获取具有小数值的 EditText 来执行数学运算

java - 在所有 Activity 按钮上 setOnClickListener() 的简单方法是什么

java - 如何纠正我的第一个 Spring Boot POM

java - Spring容器引入aspect后不注入(inject)依赖

spring - 无法启动 spring boot 应用程序