第 1 行第 1 列的错误:调用休息时文档为空 api
error on line 1 at column 1: Document is empty when I call a rest api
我有以下代码,其中有一个 html 模板,我上传了一个 xml
文件,读取文件内容,我需要 return 一个字符串值。
@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 字符串,我得到以下屏幕:
我想这个控制器 return 是一个 xml 而不是字符串,即使我设置了 public String
。如果我将 headers 更改为“text/plain”,它根本不起作用。我也试过 consumes/produces
但没有任何运气。有什么帮助吗?
编辑:
我想这个控制器 return 是一个 xml 而不是字符串,即使我设置了 public String
。如果我将 headers 更改为“text/plain”,它根本不起作用。我也试过 consumes/produces
没有任何运气。
这是一个有效的示例,但是 return 是一棵 xml 树。
String str_response = "<response><message>" + insertions_counter + " cases added out of " + total_rows_counter+"</message></response>";
因此 headers="Accept=application/xml; charset=utf-8"
,您的回复将具有以下内容类型 application/xml;charset=utf-8
。
您的浏览器正在尝试解析非 XML 响应(尽管内容类型表明是 XML),因此您收到该错误.
因此,您的响应可以是字符串,如果它是 XML 字符串。
如果您想要 return 文本,请删除 headers 或将其更改为
headers="Accept=text/plain;charset=UTF-8"
我有以下代码,其中有一个 html 模板,我上传了一个 xml
文件,读取文件内容,我需要 return 一个字符串值。
@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 字符串,我得到以下屏幕:
我想这个控制器 return 是一个 xml 而不是字符串,即使我设置了 public String
。如果我将 headers 更改为“text/plain”,它根本不起作用。我也试过 consumes/produces
但没有任何运气。有什么帮助吗?
编辑:
我想这个控制器 return 是一个 xml 而不是字符串,即使我设置了 public String
。如果我将 headers 更改为“text/plain”,它根本不起作用。我也试过 consumes/produces
没有任何运气。
这是一个有效的示例,但是 return 是一棵 xml 树。
String str_response = "<response><message>" + insertions_counter + " cases added out of " + total_rows_counter+"</message></response>";
因此 headers="Accept=application/xml; charset=utf-8"
,您的回复将具有以下内容类型 application/xml;charset=utf-8
。
您的浏览器正在尝试解析非 XML 响应(尽管内容类型表明是 XML),因此您收到该错误.
因此,您的响应可以是字符串,如果它是 XML 字符串。
如果您想要 return 文本,请删除 headers 或将其更改为
headers="Accept=text/plain;charset=UTF-8"