Spring Boot开发 之 ObjectMapper

本文的示例代码参考ObjectMapper

目录

  • 开始

  • ObjectMapper

    • Response

    • Request

  • Jackson

    • @JsonProperty

    • property-naming-strategy

开始

spring init -dweb --build gradle ObjectMapper
# cd ObjectMapper
vim src/main/java/com/example/ObjectMapper/UserController.java
package com.example.ObjectMapper;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/users")
public class UserController {
    @GetMapping
    public String getUsers() {
        return "getUsers";
    }
}
  • 测试
./gradlew bootrun

curl localhost:8080/users # 返回"getUsers"

ObjectMapper

Response

vim src/main/java/com/example/ObjectMapper/User.java
package com.example.ObjectMapper;

public class User {
    private Long id;

    private String fullName;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getFullName() {
        return fullName;
    }

    public void setId(String fullName) {
        this.fullName = fullName;
    }
}
vim src/main/java/com/example/ObjectMapper/UserController.java
# 省略了包名和导入文件

@RestController
@RequestMapping("/users")
public class UserController {
    private static List users = new ArrayList<>();

    public UserController() {
        User user = new User();
        user.setId(1L);
        user.setFullName("kevin yuan");
        users.add(user);
    }
    
    @GetMapping
    public List getUsers() {
        return users;
    }
}
  • 测试
./gradlew bootrun

curl localhost:8080/users
# 返回"[{"id":1,"fullName":"kevin yuan"}]"

Request

vim src/main/java/com/example/ObjectMapper/UserController.java
# 省略了包名和导入文件

@RestController
@RequestMapping("/users")
public class UserController {
    # 省略了未修改代码

    @PostMapping
    public List addUser(@RequestBody User user) {
        System.out.println(user);
        users.add(user);
        return users;
    }
}
  • 测试
./gradlew bootrun

curl -X POST -H "Content-type: application/json" -d '{"id":2,"fullName":"jason wang"}' localhost:8080/users
# 返回"[{"id":1,"fullName":"kevin yuan"},{"id":2,"fullName":"jason wang"}]"

Jackson

./gradlew dependencies  | grep jackson

@JsonProperty

vim src/main/java/com/example/ObjectMapper/User.java
package com.example.ObjectMapper;

import com.fasterxml.jackson.annotation.JsonProperty;

public class User {
    private Long id;

    @JsonProperty("full_name")
    private String fullName;

    # 省略了未修改代码
}
  • 测试
./gradlew bootrun

curl localhost:8080/users
# 返回"[{"id":1,"full_name":"kevin yuan"}]"

curl -X POST -H "Content-type: application/json" -d '{"id":2,"full_name":"jason wang"}' localhost:8080/users
# [{"id":1,"full_name":"kevin yuan"},{"id":2,"full_name":"jason wang"}]

property-naming-strategy

删除刚才添加的@JsonProperty注解

vim src/main/resources/application.properties
spring.jackson.property-naming-strategy=CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES
  • 测试
./gradlew bootrun

curl localhost:8080/users
# 返回"[{"id":1,"full_name":"kevin yuan"}]"

curl -X POST -H "Content-type: application/json" -d '{"id":2,"full_name":"jason wang"}' localhost:8080/users
# [{"id":1,"full_name":"kevin yuan"},{"id":2,"full_name":"jason wang"}]

参考

  • Jackson 在 Spring Boot 中的使用小结 1

  • Jackson 在 Spring Boot 中的使用小结 2

  • A Guide to Spring Framework Annotations

你可能感兴趣的:(Spring Boot开发 之 ObjectMapper)