标签:包含 格式 struct 好处 sse app details raw png
在项目开发中,目前比较流行的前后端数据传输方式是使用JSON格式,那么,使用JSON来作为前后端数据传输方式有什么好处?简单总结如下:
下面举个例子,对比XML与JSON格式之间的差异:
用XML表示中国部分省市数据
<?xml version="1.0" encoding="utf-8" ?>
<country>
<name>中国</name>
<province>
<name>黑龙江</name>
<citys>
<city>哈尔滨</city>
<city>大庆</city>
</citys>
</province>
<province>
<name>广东</name>
<citys>
<city>广州</city>
<city>深圳</city>
<city>珠海</city>
</citys>
</province>
<province>
<name>台湾</name>
<citys>
<city>台北</city>
<city>高雄</city>
</citys>
</province>
<province>
<name>新疆</name>
<citys>
<city>乌鲁木齐</city>
</citys>
</province>
</country>
用JSON表示中国部分省市数据
var country =
{
name: "中国",
provinces: [
{ name: "黑龙江", citys: { city: ["哈尔滨", "大庆"]} },
{ name: "广东", citys: { city: ["广州", "深圳", "珠海"]} },
{ name: "台湾", citys: { city: ["台北", "高雄"]} },
{ name: "新疆", citys: { city: ["乌鲁木齐"]} }
]
}
通过例子,可以看出来JSON的数据格式相对比较简单,并且也容易看懂。
通过上面的例子可以看到,在XML的数据格式,会用到很多的标签,而JSON的用到的很少,也就是说,JSON会更加轻量,相比之下,它本身的占用空间量就很少,也就是说,在相同的数据下,JSON带宽占用小,因此在数据传递上具有明显优势。
在SpringBoot的pom.xml中的spring-boot-starter-web这个依赖里,可以看到一个名为spring-boot-starter-json的依赖,而spring-boot-starter-json这个依赖中,又包含spring-boot-starter-json这个依赖,也就是说,SpringBoot默认的JSON库为jackson,本文将主要介绍jackson框架如何将常用的数据结构转成JSON格式。
spring-boot-starter-web.pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
<version>2.3.0.RELEASE</version>
<scope>compile</scope>
</dependency>
spring-boot-starter-json.pom
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk8</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-parameter-names</artifactId>
<scope>compile</scope>
</dependency>
User.java
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private Integer id;
private String userName;
private String userPassword;
}
UserController.java
@RestController
public class UserController {
/**
* 返回User对象
* @return
*/
@RequestMapping("getUser")
public User getUser() {
return new User(1,"JesseaKylin","123456");
}
/**
* 返回List
* @return
*/
@RequestMapping("getUserList")
public List<User> getUserList() {
List<User> userList = new ArrayList<>();
User user1 = new User(1,"JesseaKylin","123456");
User user2 = new User(2,"KylinJessea","654321");
userList.add(user1);
userList.add(user2);
return userList;
}
@RequestMapping("getUserMap")
public Map<String,Object> getUserMap() {
Map<String,Object> map = new HashMap<>();
User user = new User(1,"JesseaKylin","123456");
map.put("用户信息:" , user);
map.put("备注:","SpringBoot使用JSON");
return map;
}
}
运行结果:
Result.java
@Data
public class Result<T> {
private String msg;
private Integer code;
private T data;
}
ResultUtil.java
public class ResultUtil {
public static Result success(String msg, Object object) {
Result result = new Result();
result.setCode(200);
result.setMsg(msg);
result.setData(object);
return result;
}
public static Result success(String msg) {
return success(msg, null);
}
public static Result fail(String msg,Integer code){
Result result = new Result();
result.setCode(code);
result.setMsg(msg);
return result;
}
}
ResultController.java
@RestController
public class ResultController {
@RequestMapping("selectUser")
public Result<User> selectUser() {
User user = new User(1,"JesseaKylin","123456");
return ResultUtil.success("查询成功",user);
}
}
运行结果:
JacksonConfig.java
@Configuration
public class JacksonConfig {
@Bean
@Primary
@ConditionalOnMissingBean(ObjectMapper.class)
public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
ObjectMapper objectMapper = builder.createXmlMapper(false).build();
objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() {
@Override
public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
try {
jsonGenerator.writeString("");
} catch (IOException e) {
e.printStackTrace();
}
}
});
return objectMapper;
}
}
UserController.java
@RequestMapping("getUserMap")
public Map<String,Object> getUserMap() {
Map<String,Object> map = new HashMap<>();
User user = new User(1,"JesseaKylin","123456");
map.put("用户信息:" , user);
map.put("备注:",null);
return map;
}
运行结果:
点进@RestController,可以看到这个注解里面还包含了@ResponseBody、@Controller
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController {
@AliasFor(
annotation = Controller.class
)
String value() default "";
}
使用过SpringMVC的都知道,使用@Controller注解,返回的是一个视图,通过@ResponseBody注解在控制类中标记,可以将返回的内容变为JSON格式。
参考文章:
标签:包含 格式 struct 好处 sse app details raw png
原文地址:https://www.cnblogs.com/isKylin/p/12984589.html