The following example shows how by disable the USE_TRANSIENT_ANNOTATION feature, you can include @Transient pojo fields in your serialized json responses.
@SpringBootApplication
public class Application {
...
@Bean
ObjectMapper myObjectMapper() {
Hibernate5Module m = new Hibernate5Module();
m.disable(Hibernate5Module.Feature.USE_TRANSIENT_ANNOTATION);
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(m);
return mapper;
}
}
@Entity
public class SimplePojo {
@Transient
private static final String transientProperty = "SomethingToInclude";
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
public void setId(Long id) {
this.id = id;
}
public Long getId() {
return id;
}
@Transient
@JsonGetter(value = "transientProperty")
public String getType() {
return type;
}
@RestController
public class SimplePojoController {
@RequestMapping(
value = "/simple",
method = RequestMethod.GET
)
public @ResponseBody SimplePojo get()
throws Exception {
return new SimplePojo();
}
}
{
"id": 1,
"transientProperty": "SomethingToInclude"
}