springboot(2)

来源:互联网 发布:p2p网络摄像头 编辑:程序博客网 时间:2024/06/10 21:54

springboot 集成fastjson 和juint

上篇文章用到了一些注解
@Controller、@RestController、@RequestMapping,下面简单介绍一下

  1. @Controller 创建 处理 http请求的对象 其实就是 mvc中的c
  2. @RestController Spring4之后加入的注解,原来在@Controller中返回json需要@ResponseBody来配合,如果直接用@RestController替代@Controller就不需要再配置@ResponseBody,默认返回json格式。
  3. @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
原创粉丝点击