spring boot 返回 JSON 数据详解编程语言
2023-06-13 09:11:49 时间
groupId org.springframework.boot /groupId
artifactId spring-boot-starter-parent /artifactId
version 2.0.4.RELEASE /version
/parent
!--包含所有 JSON 处理的包--
dependency
groupId org.springframework.boot /groupId
artifactId spring-boot-starter-web /artifactId
/dependency
2.定义返回方式
在 Controller 类上面用 @RestController 定义或者在方法上面用 @ResponseBody 定义,表明是在 Body 区域输出数据
@RestController public class UserTest { @GetMapping(value = "/user/{userId}") public User getUserInfo(@PathVariable("userId") String userId) { User user = new User("lisi", 18); user.setId(Long.valueOf(userId)); return user; } }
3.自定义输出格式
自定义 XML 格式
[email protected]:用来自定义属性标签名称;
@JsonIgnore: 用来忽略不想输出某个属性的标签;
@JsonInclude: 用来动态包含属性的标签,如可以不包含为 null 值的属性
public class User { @JsonProperty("user-name") private String userName; private Long id; private Integer age; @JsonIgnore private String address; @JsonInclude(JsonInclude.Include.NON_NULL) private String memo; // ... get set }
4.完成对象 和 Json 的互转
jackson-databind 包里面有一个 com.fasterxml.jackson.databind.ObjectMapper 类可以完成对象和 Json 数据的互转
ObjectMapper objectMapper = new ObjectMapper(); String userJsonStr = objectMapper.writeValueAsString(user); user = objectMapper.readValue(userJsonStr, User.class);自定义输出格式
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/18564.html
cjavamacxml相关文章
- Spring Boot+Vue开发实战_有关理解的图片带字
- 你有没有掉进去过这些Spring Boot中的“陷阱“(上)
- Spring Boot 静态资源处理
- Spring Boot + @Async = 王炸!!
- 一个依赖搞定 Spring Boot 接口防盗刷
- Spring Cloud Config
- Spring Boot拦截器配置拦截登陆「建议收藏」
- spring boot整合shiro_Spring框架介绍及使用
- spring boot的基本配置——spring boot的web开发——Thymeleaf模板引擎———通过HttpSession session的session.setAttribute返回数据给
- springboot项目搭建流程_spring boot 项目
- springboot原理详解_Spring Boot
- spring boot activiti工作流_activiti工作流优缺点
- spring-boot-devtools的作用_maven compiler plugin
- 正式抛弃 Feign!Spring 6 推出新特性:HTTP Interface,这波太秀了!
- 理论:第二章:Spring的AOP和IOC是什么?使用场景有哪些?Spring事务与数据库事务,传播行为,数据库隔离级别
- Spring Boot的数据访问方式(三)
- 别再自己瞎写工具类了,Spring Boot 内置工具类应有尽有, 建议收藏!!
- 一文读懂 Spring Boot、微服务架构和大数据治理三者之间的故事详解大数据
- Spring Boot(三):logback打印日志详解编程语言
- Spring Boot 返回 JSON 数据,一分钟搞定!详解编程语言
- Spring Boot(十四):spring boot整合shiro-登录认证和权限管理详解编程语言
- Spring Boot(五):spring data jpa的使用详解编程语言
- Spring Boot(二):web综合开发详解编程语言