Send XML with namespace to REST controller in SpringBoot

461 Views Asked by At

I need to add namespace as an empty value when responding in xml in springboot.

The value of namespace should be empty (<test_response xmlns="">).

However, when the code below is executed, the namespace is not displayed.

If you know how to display namespace, please reply.

TestResponse.java

@JacksonXmlRootElement(localName = "test_response", namespace = "")
public class TestResponse {
    private String code;
    private String message;

    public TestResponse() {
    }

    public TestResponse(String code, String message) {
        this.code = code;
        this.message = message;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

TestController.java

@RestController
public class TestController {

    @PostMapping(value ="/test", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.TEXT_XML_VALUE)
    public ResponseEntity<TestResponse> test() {

        TestResponse body = new TestResponse("0000","SUCCESS");
        return ResponseEntity.ok(body);
    }
}

build.gradle

plugins {
    id 'org.springframework.boot' version '2.5.11'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

repositories {
    mavenCentral()
}

dependencies {
    implementation group: 'com.fasterxml.jackson.dataformat', name: 'jackson-dataformat-xml', version: '2.10.3'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

tasks.named('test') {
    useJUnitPlatform()
}

The response I want to send

<test_response xmlns="">
    <code>0000</code>
    <message>SUCCESS</message>
</test_response>

Current Response

<test_response>
    <code>0000</code>
    <message>SUCCESS</message>
</test_response>
0

There are 0 best solutions below