我有一个 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
对象的数组列表。
这是服务器上我的 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/