nested exception is java.lang.IllegalStateException: getWriter()
has already been called for this response] with root cause
java.lang.IllegalStateException: getWriter() has already been called
for this response
这个异常通常是因为在同一个请求中多次调用了 `response.getWriter()` 方法。在一个请求中,你只能调用一次 `getWriter()` 或 `getOutputStream()` 方法来获取对应的输出流。
如果你的代码中多次调用了 `response.getWriter().write()` 方法,你可以尝试使用一个临时变量来存储要写入响应的内容,并在最后一次调用 `write()` 方法将其写入响应中。以下是修改后的示例代码:
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
@RestController
@RequestMapping("/api")
public class YourController {
@PutMapping("/update")
public ResponseEntity<Map<String, Object>> update(@JsonParam GyShopConfig entity,
HttpSession session,
HttpServletRequest request,
HttpServletResponse response) {
HashMap<String, Object> result = new HashMap<>();
// 进行更新操作并获得更新结果
Map<String, Object> updateResult = super.update(entity, session, request, response);
// 设置响应的Content-Type和字符编码
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
try {
// 将更新结果转换为字符串
String updateResultJson = String.valueOf(updateResult);
// 写入响应中
response.getWriter().write(updateResultJson);
} catch (IOException e) {
e.printStackTrace();
}
result.put("dataList", updateResult);
return new ResponseEntity<>(result, HttpStatus.OK);
}
}
通过将更新结果保存在一个临时变量 `updateResultJson` 中,然后在最后一次调用 `write()` 方法将其写入响应中,可以解决这个异常问题。这样就确保了 `getWriter()` 方法只被调用一次,并且在响应中只写入了一次数据。