用名称空间将XML发送到Springboot中的REST控制器
在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论