我有以下代碼,其中有一個 html 模板,我上傳了一個xml檔案,讀取了檔案的內容,我需要回傳一個字串值。
@Transactional
@Component
@RestController
public class ImportE125Controller {
@Autowired
@Qualifier("jdbcImportE125")
private JdbcTemplate template;
@PostMapping(path = "/importE125", headers="Accept=application/xml; charset=utf-8", produces = "text/plain")
public String getE125New(@RequestParam("file") MultipartFile file, ModelMap modelMap,
HttpServletResponse response) throws ServletException, IOException
{
modelMap.addAttribute("file", file);
String xmlString = new String (file.getBytes(), StandardCharsets.UTF_8);
System.out.println("\n\nFILE: " xmlString);
Document doc = convertStringToDocument(xmlString);
//Normalize XML structure
doc.getDocumentElement().normalize();
//Parameters
String institutionID = null;
String institutionName = null;
int total_rows_counter = 0;
int insertions_counter = 0;
String cases_not_inserted = null;
int insert;
try {
insert = template.update("INSERT INTO tblE125Details (ForeasCode, EYear, ArProtokolou)"
" VALUES (?,?,?)",
"test", "2022", "1" );
}
catch (InvalidResultSetAccessException e)
{
throw new RuntimeException(e);
}
catch (DataAccessException e)
{
throw new RuntimeException(e);
}
total_rows_counter = total_rows_counter 1;
if (insert == 1) {
System.out.println("\nInsert Value == " insert);
insertions_counter = insertions_counter 1;
}
else if (insert == 0) {
cases_not_inserted = cases_not_inserted "IDno: " pINPersonInCompetentMemberState ", ";
}
System.out.println("\n\nNEW\n\n");
}
String str = convertDocumentToString(doc);
//return str;
String str_response = "Response: \n" insertions_counter " cases added out of " total_rows_counter;
return str_response;
}
我得到以下螢屏,而不是瀏覽器中的 str_response 字串:

我想這個控制器回傳一個 xml 而不是一個字串,即使我設定了public String. 如果我將標題更改為“文本/純文本”,它根本不起作用。我也試過consumes/produces沒有任何運氣。請問有什么幫助嗎?
編輯:
我想這個控制器回傳一個 xml 而不是一個字串,即使我設定了public String. 如果我將標題更改為“文本/純文本”,它根本不起作用。我也試過consumes/produces沒有任何運氣。
這是一個有效的示例,但回傳一個 xml 樹。
String str_response = "<response><message>" insertions_counter " cases added out of " total_rows_counter "</message></response>";
uj5u.com熱心網友回復:
因此headers="Accept=application/xml; charset=utf-8",您的回應將具有以下內容型別application/xml;charset=utf-8。
您的瀏覽器正在嘗試決議非 XML 回應(盡管內容型別指示為XML),因此您會收到該錯誤。
因此,您的回應可以是一個字串,如果它是一個 XML 字串。
如果要回傳文本,請洗掉標題或將其更改為
headers="Accept=text/plain;charset=UTF-8"
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/426465.html
