SpringBoot返回json和xml的示例代码

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<? xml version = "1.0" encoding = "UTF-8" ?>
   < 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类,代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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 [email protected]
  * @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,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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 [email protected]
  * @date 2018/4/8
  */
@RestController
public class UserController {
 
   @GetMapping (value = "/json" ,produces = MediaType.APPLICATION_JSON_VALUE)
   public User index(){
     User user = new User( "dalaoyang" , "26" , "北京" );
     return user;
   }
 
   @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,如下图


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

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

原文链接:https://www.jianshu.com/p/231b5ffa19b7

猜你喜欢

转载自blog.csdn.net/h610443955/article/details/80512982