用名称空间将XML发送到Springboot中的REST控制器

发布于 2025-01-21 14:03:16 字数 2132 浏览 3 评论 0原文

在Springboot中XML响应时,我需要添加名称空间作为空值。

名称空间的值应为空(< test_response xmlns =“”>)。

但是,当执行下面的代码时,未显示名称空间。

如果您知道如何显示名称空间,请回复。

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()
}

我要发送

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

当前响应的响应

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

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文