查看: 284|回复: 0

[Java学习] SpringBoot返回json和xml的示例代码

发表于 2018-5-5 09:31:16

有些情况接口需要返回的是xml数据,在springboot中并不需要每次都转换一下数据格式,只需做一些微调整即可。

新建一个springboot项目,加入依赖jackson-dataformat-xml,pom文件代码如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.dalaoyang</groupId>
  6. <artifactId>springboot_xml</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9. <name>springboot_xml</name>
  10. <description>springboot_xml</description>
  11. <parent>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-parent</artifactId>
  14. <version>1.5.9.RELEASE</version>
  15. <relativePath/> <!-- lookup parent from repository -->
  16. </parent>
  17. <properties>
  18. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  19. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  20. <java.version>1.8</java.version>
  21. </properties>
  22. <dependencies>
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-starter-web</artifactId>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.springframework.boot</groupId>
  29. <artifactId>spring-boot-devtools</artifactId>
  30. <scope>runtime</scope>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.springframework.boot</groupId>
  34. <artifactId>spring-boot-starter-test</artifactId>
  35. <scope>test</scope>
  36. </dependency>
  37. <dependency>
  38. <groupId>com.fasterxml.jackson.dataformat</groupId>
  39. <artifactId>jackson-dataformat-xml</artifactId>
  40. </dependency>
  41. </dependencies>
  42. <build>
  43. <plugins>
  44. <plugin>
  45. <groupId>org.springframework.boot</groupId>
  46. <artifactId>spring-boot-maven-plugin</artifactId>
  47. </plugin>
  48. </plugins>
  49. </build>
  50. </project>
复制代码

启动类默认即可,没有做任何调整。

新建一个user类,代码如下:

  1. package com.dalaoyang.entity;
  2. import javax.xml.bind.annotation.XmlElement;
  3. import javax.xml.bind.annotation.XmlRootElement;
  4. /**
  5. * @author dalaoyang
  6. * @Description
  7. * @project springboot_learn
  8. * @package com.dalaoyang.entity
  9. * @email yangyang@dalaoyang.cn
  10. * @date 2018/4/8
  11. */
  12. @XmlRootElement
  13. public class User {
  14. String userName;
  15. String userAge;
  16. String userAddress;
  17. public User(String userName, String userAge, String userAddress) {
  18. this.userName = userName;
  19. this.userAge = userAge;
  20. this.userAddress = userAddress;
  21. }
  22. @XmlElement
  23. public String getUserName() {
  24. return userName;
  25. }
  26. public void setUserName(String userName) {
  27. this.userName = userName;
  28. }
  29. @XmlElement
  30. public String getUserAge() {
  31. return userAge;
  32. }
  33. public void setUserAge(String userAge) {
  34. this.userAge = userAge;
  35. }
  36. @XmlElement
  37. public String getUserAddress() {
  38. return userAddress;
  39. }
  40. public void setUserAddress(String userAddress) {
  41. this.userAddress = userAddress;
  42. }
  43. }
复制代码

最后是controller,代码如下:

  1. package com.dalaoyang.controller;
  2. import com.dalaoyang.entity.User;
  3. import org.springframework.http.MediaType;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. import org.springframework.web.bind.annotation.RestController;
  6. /**
  7. * @author dalaoyang
  8. * @Description
  9. * @project springboot_learn
  10. * @package com.dalaoyang.controller
  11. * @email yangyang@dalaoyang.cn
  12. * @date 2018/4/8
  13. */
  14. @RestController
  15. public class UserController {
  16. //http://localhost:8080/json
  17. @GetMapping(value = "/json",produces = MediaType.APPLICATION_JSON_VALUE)
  18. public User index(){
  19. User user = new User("dalaoyang", "26", "北京");
  20. return user;
  21. }
  22. //http://localhost:8080/xml
  23. @GetMapping(value = "/xml",produces = MediaType.APPLICATION_XML_VALUE)
  24. public User XML(){
  25. User user = new User("dalaoyang", "26", "北京");
  26. return user;
  27. }
  28. }
复制代码

到这里就可以启动项目了,访问http://localhost:8080/json,可以看到如下图


访问http://localhost:8080/xml,如下图


源码下载 :https://gitee.com/dalaoyang/springboot_learn

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持程序员之家。



回复

使用道具 举报