springboot(2)
来源:互联网 发布:p2p网络摄像头 编辑:程序博客网 时间:2024/06/10 21:54
springboot 集成fastjson 和juint
上篇文章用到了一些注解
@Controller、@RestController、@RequestMapping,下面简单介绍一下
- @Controller 创建 处理 http请求的对象 其实就是 mvc中的c
- @RestController Spring4之后加入的注解,原来在@Controller中返回json需要@ResponseBody来配合,如果直接用@RestController替代@Controller就不需要再配置@ResponseBody,默认返回json格式。
- @RequestMapping:配置url映射
这篇文章主要记录一下 springboot集成junit和fastjson, log
首先 先看一下 springboot集成test
- 在test目录下随便建一个测试类,然后加上注解 @SpringBootTest即可,就这么简单
@RunWith(SpringRunner.class)@SpringBootTestpublic class CommonTest { @Test public void test () { System.out.println(123); }}
第二点 javaweb 最常用的就是json转换和 log日志,首先看一下json转换 比较倾向于阿里爸爸的 fastjson
首先gradle 添加记录
compile 'com.alibaba:fastjson:1.2.31'
然后自己写个JsonUtil
package com.naonao.util;import com.alibaba.fastjson.JSON;import org.springframework.stereotype.Component;/** * Created by naonao on 17/4/5. */@Component("fastJsonUtil")public class FastJsonUtil { public String toJson(Object object) { if (null == object) { return ""; } return JSON.toJSONString(object); }}
测试一下fastJsonUtil 可以用不
package com.naonao;import com.naonao.api.TestController;import com.naonao.util.FastJsonUtil;import com.naonao.vo.TestUser;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;/** * Created by naonao on 17/4/5. */@RunWith(SpringRunner.class)@SpringBootTestpublic class CommonTest { @Autowired private TestController testController; @Autowired private FastJsonUtil fastJsonUtil; @Test public void test () { System.out.println(123); } @Test public void testUser1() { TestUser user = new TestUser(); TestUser t = testController.test(user); System.out.println(t.getAge() + " " + t.getName()); System.out.println(fastJsonUtil.toJson(t)); }}
看一下输出
{"age":12,"name":"qwe"}
还有就是用fastjson替换boot自带的json有两种方法
1.启动类继承extends WebMvcConfigurerAdapter
package com.naonao;import com.alibaba.fastjson.serializer.SerializerFeature;import com.alibaba.fastjson.support.config.FastJsonConfig;import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.http.converter.HttpMessageConverter;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;import java.util.List;@SpringBootApplicationpublic class NaonaoServiceSoaApplication extends WebMvcConfigurerAdapter { @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { super.configureMessageConverters(converters); FastJsonHttpMessageConverter converter= new FastJsonHttpMessageConverter(); FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); converter.setFastJsonConfig(fastJsonConfig); converters.add(converter); } public static void main(String[] args) { SpringApplication.run(NaonaoServiceSoaApplication.class, args); }}
然后 在pojo TestUser做如下操作
package com.naonao.vo;import com.alibaba.fastjson.annotation.JSONField;import lombok.Data;import java.io.Serializable;/** * Created by naonao on 17/4/4. */@Datapublic class TestUser implements Serializable { private int age; private String name; @JSONField(serialize = false) private int id;}
刚刚写的测试类测试一下
{"age":12,"name":"qwe"}
如果把id上面的JsonFiled 注解去掉的话
{"age":12,"id":1,"name":"qwe"}
2 在启动类中,注入Bean : HttpMessageConverters
@Bean public HttpMessageConverters fastJsonHttpMessageConverters() { FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter(); FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); converter.setFastJsonConfig(fastJsonConfig); HttpMessageConverter<?> converter1 = converter; return new HttpMessageConverters(converter1); }
实际上两个方法异曲同工
0 0
- springboot笔记(2)springboot-Mybatis使用
- springboot(2)
- SpringBoot学习(2)
- SpringBoot基础(2)
- springboot(2):综合开发
- SpringBoot+Maven项目实战(2):集成SpringBoot
- springboot笔记(2)springboot-添加对jsp支持
- SpringBoot+Maven项目实战(2):集成SpringBoot
- SpringBoot自我整理学习2(springboot集成Druid)
- springboot 2(定时任务)
- SpringBoot入门-2(HelloWorld)
- springboot-2
- springboot(四、springboot+mybatis)
- SpringBoot--使用redis做缓存(2)
- springboot-日志处理(二)-2
- SpringBoot学习之--单元测试(八)-2
- Spring security实战(2)-----搭建SpringBoot
- 【SpringBoot】2小时学会SpringBoot学习笔记(第1章- 第2章)
- SUMO文档028:来源于观测点的路径
- 【编程题目】Pq.7--打印1到最大的n位数(关于大数问题的探讨)
- BZOJ-1854 (二分图匹配)
- 荷兰国旗(分堆)
- CSS中的子绝父相是什么意思
- springboot(2)
- 4.5
- React-native学习过程 五 登录界面
- 在MFC的MDI程序中设置子窗口客户区大小
- 留坑-那些年的术语
- keras中如何保存model的数据,以及如何重构模型进行数据预测
- 【338】Counting Bits
- 使用Canvas绘制网络设备拓扑图
- poj1011 -- Sticks (DFS+剪枝)