0

When I try to send request with xml payload I get error: Content type 'application/xml' not supported for bodyType.

I added this dependency:

implementation "com.fasterxml.jackson.dataformat:jackson-dataformat-xml"

WebClient configuration:

@Bean
fun myWebClient(logbookHttpClient: HttpClient): WebClient = WebClient.builder()
    .codecs {
        it.defaultCodecs().jaxb2Encoder(Jaxb2XmlEncoder())
        it.defaultCodecs().jaxb2Decoder(Jaxb2XmlDecoder())
        it.defaultCodecs().jackson2JsonDecoder(Jackson2JsonDecoder())
        it.defaultCodecs().jackson2JsonEncoder(Jackson2JsonEncoder())
        it.customCodecs().register(Jaxb2XmlDecoder())
        it.customCodecs().register(Jaxb2XmlEncoder())
    }
    .baseUrl(host)
    .build()

WebClient usage:

myWebClient.put()
    .uri("/blabla")
    .headers { it.addAll(combineHeaders()) }
    .contentType(MediaType.APPLICATION_XML)
    .accept(MediaType.APPLICATION_XML)
    .bodyValue(request)
    .retrieve()
    .awaitBody<MyXmlResponse>()

RequestClass:

@JacksonXmlRootElement(localName = "FIXML", namespace = "http://www.fixprotocol.org/FIXML-5-0-SP2")
data class FixmlRequest {

    @field:JacksonXmlProperty(localName = "v", isAttribute = true)
    val version = "5.0 SP2"

    @field:JacksonXmlProperty(localName = "ReqID", isAttribute = true)
    val reqId: String
}

I don't know what I'm doing wrong.

1 Answer 1

0

I found the answer. My common mistake is using Jackson annotations instead of Jaxb.

@JacksonXmlRootElement(localName = "FIXML", namespace = 
"http://www.fixprotocol.org/FIXML-5-0-SP2")
@XmlAccessorType(XmlAccessType.FIELD)
data class FixmlRequest {

  @field:XmlAttribute(name = "v")
  val version = "5.0 SP2"

  @field:XmlAttribute(name = "ReqID")
  val reqId: String? = null
}

All elements must be nullable and instantiated by value or null. All objects annotated @XmlElement must contain namespace.

Also we don't need to configure codecs in WebClient because Jaxb encoder/decoder used by default.

Not the answer you're looking for? Browse other questions tagged or ask your own question.