Spring Version: v2.0.1.RELEASE
Problem
I am running a spring boot server using RequestEntity interface. When I am doing a POST request with this data
bookdata.xml
<Book>
<name>Ashish</name>
<subject>
<name>Math</name>
<name>Hindi</name>
</subject>
</Book>
I am getting the following response .
{"name":"Ashish","subject":{"name":"Hindi"}}
The XML data is getting lost.
Here is the server code
public class GetFunctionData {
public static ResponseEntity<?> call(RequestEntity req) {
return ResponseEntity.ok(req.getBody());
}
}
Just to verify the same I ran another server with HttpServletRequest interface and there is no data loss. Server code with HttpServletRequest
@RestController
@EnableAutoConfiguration
public class Server {
@RequestMapping(value = "/", method = { RequestMethod.GET, RequestMethod.POST, RequestMethod.DELETE, RequestMethod.PUT })
ResponseEntity<Object> home(HttpServletRequest request) {
String body;
try {
// Getting headers and Converting header in MultiValueMap format.
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
Enumeration headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) {
String key = (String) headerNames.nextElement();
String value = request.getHeader(key);
headers.put(key, singletonList(value));
}
//extract the queryparameters from the request
StringBuilder newQuery = new StringBuilder(request.getRequestURL().toString());
Enumeration<String> parameterNames = request.getParameterNames();
Boolean haveQueryParameter = false;
while (parameterNames.hasMoreElements()) {
if(!haveQueryParameter) {
newQuery.append("?");
haveQueryParameter = true;
}else {
newQuery.append("&");
}
String paramName = parameterNames.nextElement();
String[] paramValues = request.getParameterValues(paramName);
for (int paramCount = 0; paramCount < paramValues.length; paramCount++) {
if(paramCount==0)
newQuery.append(paramName+"="+request.getParameterValues(paramName)[paramCount]);
else
newQuery.append("&"+paramName+"="+request.getParameterValues(paramName)[paramCount]);
}
}
URI newUri = new URI(newQuery.toString());
//Extract the body from the reqest
body = request.getReader().lines().collect(Collectors.joining());
//Get the rawrequest
RequestEntity<Object> rawRequest = new RequestEntity<Object>(body,headers,HttpMethod.resolve(request.getMethod()), newUri);
return ((ResponseEntity<Object>) GetFunctionData.call(rawRequest));
} catch (Exception e) {
e.printStackTrace();
return ResponseEntity.badRequest().body(e.getMessage());
}
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Server.class, args);
}
}
Here is the gist for the code https://gist.github.com/anubhav6663/90cf922ce4b07af2c135ca8df4e5d05e
How to reproduce the issue
Run RequestServer.java from the gist.
Using terminal:
curl -X POST -H 'Content-type:text/xml' -d @bookdata.xml http://localhost:8080
Comment From: rstoyanchev
Thanks for getting in touch, but it feels like this is a question that would be better suited to Stack Overflow. As mentioned in the guidelines for contributing, we prefer to use the issue tracker only for bugs and enhancements.
The above does not prove there is an issue. It only demonstrates that the body received is the same, which is not in doubt. What isn't clear is how the body is converted. You need to find out what library is doing the parsing, which depends on your classpath and what it is actually doing, what Object does it convert to. Keep in mind that a declaration like RequestEntity<?> does not provide any hint what target Object to convert to and that is likely the reason for the issue.