我有下面的代码,我有一个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
。如果我把头改为"text/plain",它根本不工作。我也试过consumes/produces
,但没有任何运气。请帮助我吗?
- 编辑:**
我假设这个控制器返回一个xml而不是一个字符串,即使我设置了public String
。如果我把头改为"text/plain",它根本不起作用。我也尝试了consumes/produces
,但没有任何运气。
这是一个可以工作的示例,但返回的是一个XML树。
String str_response = "<response><message>" + insertions_counter + " cases added out of " + total_rows_counter+"</message></response>";
1条答案
按热度按时间bjg7j2ky1#
由于此
headers="Accept=application/xml; charset=utf-8"
,您的响应将具有以下内容类型application/xml;charset=utf-8
。您的浏览器正在尝试解析一个非XML响应(尽管内容类型指示为 XML),因此您收到了该错误。
因此,如果响应是XML字符串,则它可以是String。
如果要返回文本,请移除标题或将其更改为
headers="Accept=text/plain;charset=UTF-8"