A tool for dynamically filtering entity properties based on Jackson.
- Add denpendency
<dependency>
<groupId>com.github.ksprider</groupId>
<artifactId>Surgical</artifactId>
<version>0.0.5</version>
</dependency>
- Enable Surgical with
@EnableJSON
@SpringBootApplication
@EnableJSON
public class RestServiceApplication {
public static void main(String[] args) {
SpringApplication.run(RestServiceApplication.class, args);
}
}
- Add annotation to method
@JSON
Object which will to be filted must implement java.io.Serializable interface.
class Tiger implements Serializable {
public int no;
public String name;
public int age;
public Zoo zoo;
}
class Zoo implements Serializable {
public String name;
public String address;
public City city;
}
class City implements Serializable {
public int id;
public String name;
}
@GetMapping("/demo0")
@JSON("no,name,age,zoo(name,address,city(id,name))")
public Tiger demo0() {
City city = new City();
city.id = 100001;
city.name = "Singapore";
Zoo zoo = new Zoo();
zoo.name = "Singapore Zoo";
zoo.address = "80 Mandai Lake Rd, Singapore 729826";
zoo.city = city;
Tiger tiger = new Tiger();
tiger.no = 1;
tiger.name = "Pasha";
tiger.age = 4;
tiger.zoo = zoo;
return tiger;
}
[{"no":1,"name":"Pasha","age":4,"zoo":{"name":"Singapore Zoo","address":"80 Mandai Lake Rd, Singapore 729826","city":{"id":100001,"name":"Singapore"}}}]
@GetMapping("/demo1")
@JSON("no,name,zoo(name,city(name))")
public Tiger demo1() {
// same with above
return tiger;
}
{"no":1,"name":"Pasha","zoo":{"name":"Singapore Zoo","city":{"name":"Singapore"}}}
@GetMapping("/demo2")
@JSON("no,name")
public Tiger demo2() {
// same with above
return tiger;
}
{"no":1,"name":"Pasha"}
@GetMapping("/demo3")
@JSON("no,name,zoo(name,city(*))")
public Tiger demo3() {
// same with above
return tiger;
}
{"no":1,"name":"Pasha","zoo":{"name":"Singapore Zoo","city":{"id":100001,"name":"Singapore"}}}