我想使控制器中的方法Spring MVC方法返回text
而不是json
。使mvc控制器返回文本,而不是json
我现在的方法是这样的
@RequestMapping(value = "/upload", method = RequestMethod.POST, produces = "text/html")
public ModelAndView uploadFile(@RequestParam("file") MultipartFile file) {
LOGGER.debug("Attempt to upload file with template.");
try {
String fileContent = FileProcessUtils.processFileUploading(file);
return createSuccessResponse(fileContent);
} catch (UtilityException e) {
LOGGER.error("Failed to process file.", e.getWrappedException());
return createResponse(INTERNAL_ERROR_CODE, e.getMessage());
}
}
但响应头content-type: application/json
。
我试图通过HttpServletResponse
控制器和设置内容类型,但它仍然继续返回json
。
有什么问题?
user'responseEntity'而不是'ModelAndView'?第二种解决方案不起作用 – lapots
那么'ResponseEntity'工作正常。毕竟,你正在进行http调用。 – mtyurt