0
点赞
收藏
分享

微信扫一扫

【SpringMVC】06.Json【个人用笔记】

曾宝月 2022-04-16 阅读 37
javaspring

文章目录

方法1:jackson-databind

1,转换成json发给前台

导入pom

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.13.2.2</version>
        </dependency>

java代码

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
// @RestController:(类下所有方法全局JSON)
@RequestMapping("/hello")
public class Demo1 {
	// 如果中文发生乱码的话 @RequestMapping(value="/test3",produces="text/html;charset=utf-8")
    @RequestMapping("/test1")
    @ResponseBody
    public User test1() {
        return new User("五河士道",16);
    }
}

输出结果

{"name":"五河士道","age":16}

2,前端发json给后台

        var user = {name:"五河士织", age:"16"};
        var userJson = JSON.stringify(user);

3,后台接收json

    @RequestMapping("/test2")
    public User test2(@RequestBody User user) {
        System.out.println(user);
        return user;
    }

4,关键字总结

# RestController:
	类下所有方法都会return json形式
	使用的时候要删除 @Controller 注解
# ResponseBody:
	只针对某一个方法
# ReuqestBody:
	接收从前端来的json(前端要post)

补充注解

1,日期格式化

    @JsonFormat(pattern = "yyyy年MM月dd日 HH:mm:ss",timezone = "GMT-8")
    private Date bitrh;

2,禁止某一属性转换json

	@JsonIgnore
	private String name;

3,禁止为空的属性转换json

	@JsonInclude(JsonInclude.Include.NON_NULL) // 其他的属性也可以试试,比如数组用empty
	private String name;

4,属性名改名

	@JsonProperty("name2")
	private String name;

5,序列化

定义序列化规范

public class MySerialize extends JsonSerializer<Double> {
    @Override
    public void serialize(Double aDouble, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        // 四舍五入
        String number = BigDecimal.valueOf(aDouble).setScale(2,BigDecimal.ROUND_HALF_UP).toString();
        // 输出四舍五入的值
        jsonGenerator.writeNumber(number);
    }
}

属性注解设置:

    @JsonSerialize(using = MySerialize.class)
    private Double height = 3.1415926;

方法2:alibaba fastjson

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.80</version>
        </dependency>

配置xml

    <!-- 注解驱动 -->
    <mvc:annotation-driven>
        <mvc:message-converters>
            <!-- 阿里巴巴json -->
            <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>application/json</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

可以用的注解

    @JSONField(name="name2")
    @JSONField(serialize = false)
    @JSONField(format = "yyyy年MM月dd日 HH:mm:ss")
    @JSONField(serialzeFeatures = SerializerFeature.WriteMapNullValue)
    @JSONField(serialzeFeatures = SerializerFeature.WriteNullStringAsEmpty)
    @JSONField(serializeUsing = MySerialize2.class)
举报

相关推荐

0 条评论