해결하고 싶은 것

아래와 같은 내용을 Spring @RequestBody 에 값을 전달하고싶다.

{
    "zooName":"MorrisZoo",
    "animals":[
        {"type":"cat", "name":"yammi", "likesCream":true,"lives":0},
        {"type":"dog", "name":"Fancy","barkVolume":4}
    ]
}

animals는 Animal 타입을 상속 받은 dog, cat 의 객체로 받아와야한다.

구현하기
Jackson Polymorphic Type Handling Annotations 을 참고
Jackson 라이브러리는 다음에 한번에 정리를 하겠습니다.

AnimalController.java

@RestController
public class AnimalController {

    @PostMapping(value="/zooinfo")
    public void jsonTypeCheck(@RequestBody Zoo zoo){
        String zooName = zoo.zooName;
        System.out.println("동물원 이름 : "+zooName);
        zoo.animals.forEach((value)->value.println());
    }
}

Zoo.java : @RequestBody에 받아올 zoo 객체

package com.jackson.jacksontutorial;

import java.util.List;

public class Zoo {
    public String zooName;
    public List<Animal> animals;
}

Animal.java` : Dog, Cat의 상위 클래스

package com.jackson.jacksontutorial;

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;

@JsonTypeInfo(
        use = JsonTypeInfo.Id.NAME,
        include = JsonTypeInfo.As.PROPERTY,
        property = "type")
@JsonSubTypes({
        @JsonSubTypes.Type(value = Dog.class, name = "dog"),
        @JsonSubTypes.Type(value = Cat.class, name = "cat")
})
public abstract class Animal {
    public String name;

    public void println( ){
        System.out.println("Animal Type : "+name);
    };

}

Cat.java

package com.jackson.jacksontutorial;

import com.fasterxml.jackson.annotation.JsonRootName;
import com.fasterxml.jackson.annotation.JsonTypeName;


//@JsonTypeName("cat")
@JsonRootName("cat")
public class Cat extends Animal {
    boolean likesCream;
    public int lives;

    @Override
    public String toString() {
        return "Cat{" +
                "likesCream=" + likesCream +
                ", lives=" + lives +
                '}';
    }

    @Override
    public void println() {
        String AnimalType = super.name;
        System.out.println("name : "+name+","+this.toString());
    }
}

Dog.java

package com.jackson.jacksontutorial;

import com.fasterxml.jackson.annotation.JsonRootName;
import com.fasterxml.jackson.annotation.JsonTypeName;

//@JsonTypeName("dog")
@JsonRootName("dog")
public class Dog extends Animal {
    public double barkVolume;

    @Override
    public String toString() {
        return "Dog{" +
                "barkVolume=" + barkVolume +
                '}';
    }
    @Override
    public void println() {
        String name = super.name;
        System.out.println("name : "+name+","+this.toString());
    }
}

결과

요청

post- @RequestBody

{
    "zooName":"MorrisZoo",
    "animals":[
        {"type":"cat", "name":"yammi", "likesCream":true,"lives":0},
        {"type":"dog", "name":"Fancy","barkVolume":4}
    ]
}

SpringApplication

2020-02-28 16:51:14.376  INFO 17268 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 5 ms
동물원 이름 : MorrisZoo
name : yammi,Cat{likesCream=false, lives=0}
name : Fancy,Dog{barkVolume=4.0}

+ Recent posts