Client is sending encoded payload (percent encoded), while the given code works for a non encoded payload/XML (content-type:application/xml) it fails with a http 415 for encoded payload/XML file (Content-type: application/x-www-form-urlencoded).
I noticed that FormHttpMessageReader.java successfully decodes the payload (seen in the debug logs) but somewhere in the request flow after wards my code fails to map decoded XML correctly to the POJO.
Curl request -
curl -H 'Content-type: application/x-www-form-urlencoded' http://localhost:8081/api/v1/app/test -d @encodedpyaload.xml -i-H 'Accept: application/xml, text/xml, application/x-www-form-urlencoded, */*’
HTTP/1.1 100 Continue
HTTP/1.1 415 Unsupported Media Type
Content-Type: application/json
Content-Length: 158
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
Referrer-Policy: no-referrer
{"error":"415 UNSUPPORTED_MEDIA_TYPE \"Content type 'application/x-www-form-urlencoded' not supported for bodyType=test.package.Pojo\””}
Configuration class -
@Bean
public RouterFunction<ServerResponse> testRoute() {
return route(POST(“/app/test”).and(contentType(MediaType.APPLICATION_FORM_URLENCODED)),
handler::testHandler);
}
Handler Class
public Mono<ServerResponse> testHandler(final ServerRequest request) {
return dataManager.testSave(request.bodyToMono(Pojo.class))
.flatMap(uri -> ServerResponse.ok().build())
.switchIfEmpty(ServerResponse.badRequest().build());
}
Updated Handler class code
Handler Class
return dataManager.testSave(request.bodyToMono(String.class)
.flatMap(body -> Mono.just(URLDecoder.decode(body, StandardCharsets.UTF_8))
.map(s -> createBody(s))).cast(Pojo.class))
.flatMap(uri -> ServerResponse.ok().build())
.switchIfEmpty(ServerResponse.badRequest().build());
private Pojo createBody(String s) {
XmlMapper mapper = new XmlMapper();
Pojo pojo = new Pojo();
try {
pojo = mapper.readValue(s, Pojo.class);
} catch (IOException e) {
e.printStackTrace();
}
return pojo;
}
POJO class
@XmlRootElement(name = “root”)
public class Pojo {
@XmlElement(name = “id”)
public long getId() {
return id;
}
public void setId(long id) {
this.id= id;
}
}
application/xml
set as the content type. and usebodyToMono
but i doubt that will work, or you process the body as a string and then convert the percent encoded data to standard encoding and parse the data. Why would you even send percentage encoded data as xml? why? – ToerktumlareURLDecoder.decode(percentencodedstring, "UTF-8")
– Otenrequest.exchange.getRequest().getBody()
. – Otenrequest.bodyToMono(String.class).flatMap(body -> Mono.just(URLDecoder.decode(body, "UTF-8")).map( // parse your decoded string )
– ToerktumlaredataManager.testSave()
expects aMono<Pojo>
– Oten