I have the following code where I have an html template, I upload an xml
file, read content of the file and I need to return a string value.
@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;
}
Instead of str_response string in my browser I get the following screen:
I suppose that this controller returns an xml and not a string, even though I set public String
. In case I change the headers to "text/plain" it's not working at all. I also tried consumes/produces
without any luck. Any help please?
Edit:
I suppose that this controller returns an xml and not a string, even though I set public String
. In case I change the headers to "text/plain" it's not working at all. I also tried consumes/produces
without any luck.
This is an example that works, but returns an xml tree.
String str_response = "<response><message>" insertions_counter " cases added out of " total_rows_counter "</message></response>";
CodePudding user response:
Because of this headers="Accept=application/xml; charset=utf-8"
, your response will have the following content type application/xml;charset=utf-8
.
You browser is attempting to parse an non XML response (although the content type indicates that is XML), and therefore you receive that error.
So, your response can be a String, if it is a XML string.
If you want to return text, remove the headers or change it to
headers="Accept=text/plain;charset=UTF-8"