Spring Boot 菜鸟教程 24 返回XML

编程知识 行业动态 更新时间:2024-06-13 00:22:05

GitHub

src="//ghbtns/github-btn.html?user=je-ge&repo=spring-boot&type=watch&count=true" scrolling="0" width="110" height="20">

需求产生

一般RESTful都是返回json,有时候可能需要返回xml,那又怎样操作呢?

方案1-Jackson

Maven增加jar文件导入

<dependency>
   <groupId>com.fasterxml.jackson.dataformat</groupId>
   <artifactId>jackson-dataformat-xml</artifactId>
</dependency>
<dependency>
   <groupId>org.codehaus.woodstox</groupId>
   <artifactId>woodstox-core-asl</artifactId>
   <version>4.1.0</version>
</dependency>
<dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
        </dependency>

模型对象

@JacksonXmlRootElement(localName = "user")
public class User {
  private Long id;
  @JacksonXmlCData
  @JacksonXmlProperty(localName = "Content")
  private String content;

注解说明

@JacksonXmlRootElement注解中有localName属性,该属性如果不设置,那么生成的XML最外面就是
<User></User>,而不是<user></user>

@JacksonXmlCData注解是为了生成

<![CDATA[text]]>
这样的数据,如果你不需要,可以去掉

@JacksonXmlProperty注解通常可以不需要,但是如果你想要你的xml节点名字,首字母大写。比如例子中的Content,那么必须加这个注解,并且注解的localName填上你想要的节点名字。最重要的是!实体类原来的属性content必须首字母小写!否则会被识别成两个不同的属性。

有了上面的配置,在Controller返回实体类的时候,就会像转换Json一样,将实体类转换为xml对象了。有时候你的浏览器并不能识别xml,是因为你返回的content-type不是xml,可以通过修改@RequestMapping注解的produces属性来修改:

输出xml

@RequestMapping(value = "/user", method = RequestMethod.GET, produces = { "application/xml" })
@ResponseBody
public User user() {
  return new User(1L, "JE-GE");
}

提交xml

同样的,你也可以将xml请求自动转换为实体:

@RequestMapping(value = "/user", method = RequestMethod.POST, consumes = { "text/xml" }, produces = {
      "application/xml" })
  @ResponseBody
  public User post(@RequestBody User user) {
    return user;
  }

JAXB相关的重要Annotation

@XmlRootElement:表示映射到根目录标签元素
@XmlElement:表示映射到子元素
@XmlAttribute:表示映射到属性
@XmlElementWrapper :表示类型是集合元素的子元素的上层目录
注:@XmlElementWrapper仅允许出现在集合属性上。

UserControllerTest

/**
 * @author JE哥
 * @email 1272434821@qq
 * @description:
 */
public class UserControllerTest {

  @Test
  public void testPost() throws Exception {
    // 直接字符串拼接
    StringBuilder builder = new StringBuilder();
    // xml数据存储
    builder.append("<user><id>1</id><Content>JE-GE</Content></user>");
    String data = builder.toString();
    System.out.println(data);
    String url = "http://localhost:8080/user";
    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost(url);
    httpPost.setEntity(new StringEntity(data, "text/xml", "utf-8"));
    CloseableHttpResponse response = httpClient.execute(httpPost);
    HttpEntity entity = response.getEntity();
    String content = EntityUtils.toString(entity);
    System.out.println("content:" + content);
  }
}

方案2等待

源码地址

https://github/je-ge/spring-boot

如果觉得我的文章或者代码对您有帮助,可以请我喝杯咖啡。
您的支持将鼓励我继续创作!谢谢!

更多推荐

Spring Boot 菜鸟教程 24 返回XML

本文发布于:2023-04-01 16:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/8cafe1adf9eee18bad7ea285c2a37607.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:菜鸟   教程   Boot   Spring   XML

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!