I want to program an api rest call to update a String of a class, which contains the filename of an xml file.
I am trying to do it with a GET call... But there may be a more suitable option.
This is a url sample: http://localhost/changeXML?configFile=Configuration.xml
@RequestMapping(value = "/changeXML",params= {"configFile"}, produces = { MediaType.APPLICATION_XML_VALUE},
headers = "Accept=application/xml",method = RequestMethod.GET)
public ResponseEntity<?> updateConfigFile(@RequestParam("configFile") String file) {
File f = new File(file);
System.out.println(f);
if(f.exists() && !f.isDirectory()) { //file is updated if and only if it exisits
System.out.println("FICHERO SI QUE EXISTEEEEE");
this.configFile=file;
return new ResponseEntity<String>("XML configuration file has been updated to: "+file, HttpStatus.OK);
}
System.out.println("PETITION");
//otherwise path is not going to be updated
return new ResponseEntity<String>("Unexisting XML", HttpStatus.OK);
}
All I want is the attribute configFile updated. But still, all I ever fin is the following error: This page contains the following errors: error on line 1 at column 1: Document is empty Below is a rendering of the page up to the first error.
My xml I can assure it to be fine and... even if I put this other url, http://localhost/changeXML?configFile=c%C3%B1dlvm%C3%B1ldfmv I still have the same error.
Could someone provide some info about this? Thanks in advance!
Within your @RequestMapping
annotation, you've put the MediaType.APPLICATION_XML_VALUE
value for the produces
parameter. This means that you tell your browser that the response will contain XML.
However, if you take a look at the responses, you're returning plain text in stead. Your browser probably tries to parse this as XML, but can't, and throws an error.
The solution is to tell your browser that you're returning plain text, which is the text/plain
media type, or MediaType.TEXT_PLAIN
in Spring:
@RequestMapping(
value = "/changeXML",
params= {"configFile"},
produces = {MediaType.TEXT_PLAIN}, // Change this
headers = "Accept=application/xml",
method = RequestMethod.GET)
In this case, you probably can leave away the produces
parameter entirely, as Spring will be able to automatically resolve this. Even more, the headers
and params
parameters aren't necessary either in this case, so you could just write:
@RequestMapping(value = "/changeXML", method = RequestMethod.GET)
Or even shorter:
@GetMapping("/changeXML")