SpringBoot返回json和xml的示例代码
时间:2020-10-21 21:01:31|栏目:JAVA代码|点击: 次
有些情况接口需要返回的是xml数据,在springboot中并不需要每次都转换一下数据格式,只需做一些微调整即可。
新建一个springboot项目,加入依赖jackson-dataformat-xml,pom文件代码如下:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.dalaoyang</groupId> <artifactId>springboot_xml</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springboot_xml</name> <description>springboot_xml</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-xml</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
启动类默认即可,没有做任何调整。
新建一个user类,代码如下:
package com.dalaoyang.entity; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; /** * @author dalaoyang * @Description * @project springboot_learn * @package com.dalaoyang.entity * @email yangyang@dalaoyang.cn * @date 2018/4/8 */ @XmlRootElement public class User { String userName; String userAge; String userAddress; public User(String userName, String userAge, String userAddress) { this.userName = userName; this.userAge = userAge; this.userAddress = userAddress; } @XmlElement public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } @XmlElement public String getUserAge() { return userAge; } public void setUserAge(String userAge) { this.userAge = userAge; } @XmlElement public String getUserAddress() { return userAddress; } public void setUserAddress(String userAddress) { this.userAddress = userAddress; } }
最后是controller,代码如下:
package com.dalaoyang.controller; import com.dalaoyang.entity.User; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * @author dalaoyang * @Description * @project springboot_learn * @package com.dalaoyang.controller * @email yangyang@dalaoyang.cn * @date 2018/4/8 */ @RestController public class UserController { //http://localhost:8080/json @GetMapping(value = "/json",produces = MediaType.APPLICATION_JSON_VALUE) public User index(){ User user = new User("dalaoyang", "26", "北京"); return user; } //http://localhost:8080/xml @GetMapping(value = "/xml",produces = MediaType.APPLICATION_XML_VALUE) public User XML(){ User user = new User("dalaoyang", "26", "北京"); return user; } }
到这里就可以启动项目了,访问http://localhost:8080/json,可以看到如下图
访问http://localhost:8080/xml,如下图
上一篇:通过Java代码技巧改善性能
栏 目:JAVA代码
下一篇:关于集合和字符串的互转实现方法
本文标题:SpringBoot返回json和xml的示例代码
本文地址:http://www.codeinn.net/misctech/14536.html
阅读排行
- 1Java Swing组件BoxLayout布局用法示例
- 2java中-jar 与nohup的对比
- 3Java邮件发送程序(可以同时发给多个地址、可以带附件)
- 4Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type异常
- 5Java中自定义异常详解及实例代码
- 6深入理解Java中的克隆
- 7java读取excel文件的两种方法
- 8解析SpringSecurity+JWT认证流程实现
- 9spring boot里增加表单验证hibernate-validator并在freemarker模板里显示错误信息(推荐)
- 10深入解析java虚拟机