我想使控制器中的方法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