Jackson module to handle abstract aggregate root and its subclasses in Spring Data REST - jackson

I have Spring Data REST based application with repository
public interface CriterionRepository extends JpaRepository<Criterion, Long> {
}
whereas Criterion is base class:
#Entity
#Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class Criterion extends AbstractEntity {}
and NameCriterion is its subclass
#Entity
public class NameCriterion extends Criterion {
private final String name;
}
Spring Data REST exports the repository as REST resource and one can access it at http://localhost:8080/api/criteria/
Exported resource looks as follows:
{
"_embedded": {
"nameCriteria": [{
"_links": {
"self": {
"href": "http://localhost:8080/api/nameCriterion/1"
},
"nameCriterion": {
"href": "http://localhost:8080/api/nameCriterion/1"
}
}
}
]
},
"_links": {
"self": {
"href": "http://localhost:8080/api/criteria"
},
"profile": {
"href": "http://localhost:8080/api/profile/criteria"
}
},
"page": {
"size": 20,
"totalElements": 1,
"totalPages": 1,
"number": 0
}
}
When I try to follow self link, there is no mapping for http://localhost:8080/api/nameCriterion/1
I can follow http://localhost:8080/api/criteria/1 though and I get response without name field from NameCriterion
{
"_links": {
"self": {
"href": "http://localhost:8080/api/nameCriterion/1"
},
"nameCriterion": {
"href": "http://localhost:8080/api/nameCriterion/1"
}
}
}
My assumption is it is a problem with Jackson mapper defined in REST exporter which is not tweaked correctly to handle abstract class Criterion used in JpaRepository as aggregate root.
What Jackson customization should I apply to make it working properly?
In other words, what Jackson module should I create?

There is no need to create a Jackson module. To use a single table for inherited entities we can use #RestResource annotation to mark them as the same resources:
#Entity
#Inheritance(strategy = InheritanceType.SINGLE_TABLE)
#Table(name = "criteria")
public abstract class Criterion extends AbstractEntity {
}
#RestResource(rel = "criteria", path = "criteria")
#Entity
public class NameCriterion extends Criterion {
private String name;
}
#RestResource(rel = "criteria", path = "criteria")
#Entity
public class TitleCriterion extends Criterion {
private String title;
}
#RepositoryRestResource(path = "criteria", collectionResourceRel = "criteria", itemResourceRel = "criterion")
public interface CriterionRepository extends JpaRepository<Criterion, Long> {
}
So it becomes possible to obtain all the resources (NameCriterion and TitleCriterion) in one output:
GET http://localhost:8080/api/criteria
{
"_embedded": {
"criteria": [
{
"name": "name1",
"_links": {
"self": {
"href": "http://localhost:8080/api/criteria/1"
},
"nameCriterion": {
"href": "http://localhost:8080/api/criteria/1"
}
}
},
{
"title": "title1",
"_links": {
"self": {
"href": "http://localhost:8080/api/criteria/2"
},
"titleCriterion": {
"href": "http://localhost:8080/api/criteria/2"
}
}
}
]
}
}
GET http://localhost:8080/api/criteria/1
{
"name": "name1",
"_links": {
"self": {
"href": "http://localhost:8080/api/criteria/1"
},
"nameCriterion": {
"href": "http://localhost:8080/api/criteria/1"
}
}
}
GET http://localhost:8080/api/criteria/2
{
"title": "title1",
"_links": {
"self": {
"href": "http://localhost:8080/api/criteria/2"
},
"titleCriterion": {
"href": "http://localhost:8080/api/criteria/2"
}
}
}
Working example.

Related

How to returning only as string column in json response object from eloquent relationship using eager loading

I'm trying to figure out how to eager load data as a string instead of array object from a related table.
I have 3 models and here are the relations
Product.php
class Product extends Model
{
public function ProductTag()
{
return $this->hasMany(ProductTag::class);
}
public function Category()
{
return $this->belongsTo(Category::class);
}
}
ProductTag.php
class ProductTag extends Model
{
public function Product()
{
return $this->belongsTo(Product::class);
}
}
Category.php
class Category extends Model
{
public function Product()
{
return $this->hasMany(Product::class);
}
}
I've tried doing it like so:
public function tag(){
return $this->hasMany(ProductTag::class)
->selectRaw('GROUP_CONCAT(tag) as tag,id')
->groupBy('id');
}
public static function list(){
$result = Category::with(['Product'=>function($q){
$q->with(['tag'=>function($q1){
$q1->first();
}]);
}])->get();
}
Here is the reponse:
{
"data": {
"categories": [
{
"id": 1,
"category": "test 1",
"product": [
{
"id": 46,
"name": "test prod 1",
"tag": []
},
{...}
]
},
{
"id": 2,
"category": "test 2",
"product": [
{
"id": 45,
"name": "test prod 2",
"tag": [
{
"product_tag": "Test1, test12, test123"
}
]
},
{...}
]
},
{
"id": 3,
"category": "test 3",
"product": []
}
]
}
}
The Response is as expected except tag array so, instead of an array named "tag" can I get "product_tag" within the "product" array
"product": [
{
"id": 45,
"name": "test prod 2",
"tag": [
{
"product_tag": "Test1, test12, test123"
}
]
}
]
Here is what I want it to look like:
"product": [
{
"id": 45,
"name": "test prod 2",
"product_tag": "Test1, test12, test123"
}
]
Is it possible and any smart way of doing this in Laravel using Eloquent?
Simple :)
Btw, if you can - rename product_tag in response to tag_line or same - it's not right way to take same name for relation and mutator.
class Product extends Model
{
public function getTagLineAttribute()
{
//tag - is "name" field in ProductTag
return $this->ProductTag->pluck('tag')->implode(',');
}
public function ProductTag()
{
return $this->hasMany(ProductTag::class);
}
public function Category()
{
return $this->belongsTo(Category::class);
}
}
//eager loading with tags
Product::with('ProductTag')->find(..)->tagLine;

Use a projection to modify only one field but keeping default for all others

First of all I'd like to say I love what i've seen so far from Spring Data JPA and Spring Data REST. Thanks a lot to all people involved.
Problem description
I have an entity model similar to the classes below. One parent and two different child entities referencing the parent als a ManyToOne Assoziation. For one of the childs i like to have the default rendering of all its properites and links as it is when no projection is applied to the parent.
The other child should be mapped to a simple string array containing only the id or some specific field.
Code and example JSONs
#Entity
public class Parent {
#Id
private Long id;
private String parentValue;
#OneToMany(mappedBy = "parent")
private List<Child1> child1;
#OneToMany(mappedBy = "parent")
private List<Child2> child2;
// ... getters and setters
}
#Entity
public class Child1 {
#Id
private Long id;
private String child1Value;
#ManyToOne
Parent parent;
// ... getters and setters
}
#Entity
public class Child2 {
#Id
private Long id;
#ManyToOne
Parent parent;
}
the response when getting the collection resource of parent is this
{
"_embedded": {
"parents": [
{
"parentValue": "Parent1",
"child1": [
{
"child1Value": "Child1",
"_links": {
"parent": {
"href": "http://localhost:8080/parents/1"
}
}
}
],
"child2": [
{
"_links": {
"parent": {
"href": "http://localhost:8080/parents/1"
}
}
}
],
// removed remaining json to shorten the example
}
But what i like to achieve is the following JSON
{
"_embedded": {
"parents": [
{
"parentValue": "Parent1",
"child1": [
{
"child1Value": "Child1",
"_links": {
"parent": {
"href": "http://localhost:8080/parents/1"
}
}
}
],
"child2": [1],
What i tried so far
Added an excerptProjection to the ParentRepository:
#RepositoryRestResource(excerptProjection = ParentRepository.ArrayProjection.class)
public interface ParentRepository extends PagingAndSortingRepository<Parent, Long>{
public interface ArrayProjection {
String getParentValue();
List<Child1> getChild1();
#Value("#{target.child2.![id]}")
List<Long> getChild2();
}
}
Edited: In the first version of the question, the Projection was incorrect regarding the return type of getChild1(), as it should return the complete collection not only one element. Thanks #kevvvvyp for still trying to help.
The result is similar to what i want, but the links on the Child1 property are missing now:
{
"_embedded": {
"parents": [
{
"child2": [
2
],
"child1": {
"child1Value": "Child1"
},
"parentValue": "Parent1",
// removed remaining json to shorten example
Also the approach with the excerptProjection means i'd have to change the projection everytime the entity changes. Which probalby won't happen to much but that means somebody will forget to change it in the future ;-)
I think a projection is the right way to go here, what about...
interface ArrayProjection {
#JsonIgnore
#Value("#{target}")
Parent getParent();
default String getParentValue(){
return this.getParent().getParentValue();
}
default Child1 getChild1(){
//TODO what are we actually trying to return here? Given we join the parent to
// the child on id & id is the primary key on both entities can we ever get more than one child?
return CollectionUtils.firstElement(this.getParent().getChild1());
}
default List<Long> getChild2() {
return this.getParent().getChild2().stream()
.map(Child2::getId)
.collect(Collectors.toList());
}
}
Response...
GET http://localhost:8080/api/parents
HTTP/1.1 200
Vary: Origin
Vary: Access-Control-Request-Method
Vary: Access-Control-Request-Headers
Content-Type: application/json
Transfer-Encoding: chunked
Date: Wed, 31 Mar 2021 21:08:54 GMT
Keep-Alive: timeout=60
Connection: keep-alive
{
"_embedded": {
"parents": [
{
"parentValue": "Parent1",
"child1": {
"child1Value": "Child1"
},
"child2": [
1
],
"_links": {
"self": {
"href": "http://localhost:8080/api/parents/1"
},
"parent": {
"href": "http://localhost:8080/api/parents/1{?projection}",
"templated": true
}
}
}
]
},
"_links": {
"self": {
"href": "http://localhost:8080/api/parents"
},
"profile": {
"href": "http://localhost:8080/api/profile/parents"
}
},
"page": {
"size": 20,
"totalElements": 1,
"totalPages": 1,
"number": 0
}
}
Response code: 200; Time: 713ms; Content length: 724 bytes
In reponse to your concern, if we code using default method's we will see a compile error when that entity changes. It might be possible to use a class projection instead.
Also I would consider if we really want to return a projection by default... it might confuse a client who then tries to create/update a parent (this is perhaps why you've kept the id's hidden by default?).
Sample - https://github.com/kevvvvyp/sdr-sample
Update
Another (more complex) way to acheive this could be to make use of Jackson's SimpleBeanPropertyFilter, you could customise the JSON response based on some value (the entity class, a value in a particular field, an annotation etc).

How to fix: AtlassianHostUser is null in Jira cloud plugin

How to fix: AtlassianHostUser is null in Jira cloud plugin.
My controller looks like this
#Controller
#IgnoreJwt
public class MainController {
private final StorageService storageService;
#Autowired
public MainController(StorageService storageService) {
this.storageService = storageService;
}
.
.
.
#RequestMapping(value = "/upload", method = RequestMethod.POST)
public String handleFileUpload(#AuthenticationPrincipal
AtlassianHostUser hostUser,
#RequestParam("file") MultipartFile file,
RedirectAttributes redirectAttributes) {
hostUser.getUserKey(); // Causing NPE because hostUser is null
}
My descriptor is shown below
{
"key": "copy-paste-plugin",
"baseUrl": "${addon.base-url}",
"name": "Copy Paste (Spring Boot)",
"authentication": {
"type": "jwt"
},
"lifecycle": {
"installed": "/installed",
"uninstalled": "/uninstalled"
},
"scopes": [
"READ",
"ACT_AS_USER"
],
"modules": {
"generalPages": [
{
"url": "/upload",
"key": "upload",
"location": "none",
"name": {
"value": "Attach a file"
},
"conditions": [
{
"condition": "user_is_logged_in"
}
]
}
]
}
}
This is Jira cloud plugin. I am not sure what part of it I am missing.
Looking for your help/pointer on this.
Thank you!

Save entity which has one-to-one mapping in spring data rest

I have a very simple spring boot + spring data rest app. I tried to save the entity which has a one-to-one mapping using spring data rest but looks like only the parent is saved, the child is not.
Below is my code
#SpringBootApplication
public class Application{
#Autowired
private PersonRepository personRepo;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
#Bean
CommandLineRunner init(){
Address address = new Address();
address.setCountry("US");
address.setState("SV");
Person person = new Person();
person.setName("Vincent");
person.setAddress(address);
personRepo.save(person);
return null;
}
}
#Entity
public class Address implements Serializable{
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue
private int id;
private String country;
private String state;
}
#Entity
public class Person implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue
private int id;
private String name;
#OneToOne(cascade={CascadeType.ALL})
private Address address;
}
#Projection(name="inlineAddress",types={Person.class})
public interface InlineAddress {
String getName();
Address getAddress();
}
#RepositoryRestResource(excerptProjection=InlineAddress.class)
public interface PersonRepository extends JpaRepository<Person, Integer> {
Person findByName(#Param("name") String name);
Person findById(#Param("id") int id);
Page<Person> findByNameStartsWith(#Param("name") String name, Pageable page);
}
public interface AddressRepository extends JpaRepository<Address, Integer> {
}
After startup, if I visit http://localhost:8080/api/
I saw below response
{
_links: {
addresses: {
href: "http://localhost:8080/api/addresses{?page,size,sort}",
templated: true
},
persons: {
href: "http://localhost:8080/api/persons{?page,size,sort,projection}",
templated: true
},
profile: {
href: "http://localhost:8080/api/profile"
}
}
}
Then I visit http://localhost:8080/api/persons, so far every thing is good
{
"_embedded": {
"persons": [
{
"address": {
"country": "US",
"state": "SV"
},
"name": "Vincent",
"_links": {
"self": {
"href": "http://localhost:8080/api/persons/1"
},
"person": {
"href": "http://localhost:8080/api/persons/1{?projection}",
"templated": true
},
"address": {
"href": "http://localhost:8080/api/persons/1/address"
}
}
}
]
},
"_links": {
"self": {
"href": "http://localhost:8080/api/persons"
},
"profile": {
"href": "http://localhost:8080/api/profile/persons"
},
"search": {
"href": "http://localhost:8080/api/persons/search"
}
},
"page": {
"size": 20,
"totalElements": 1,
"totalPages": 1,
"number": 0
}
}
However, after I did a post http://localhost:8080/api/persons/ with
{
"name": "Michael",
"address": {
"country": "US",
"state": "SV"
}
}
It shows below, looks like the address is not get inserted for Michael
{
"_embedded": {
"persons": [
{
"address": {
"country": "US",
"state": "SV"
},
"name": "Vincent",
"_links": {
"self": {
"href": "http://localhost:8080/api/persons/1"
},
"person": {
"href": "http://localhost:8080/api/persons/1{?projection}",
"templated": true
},
"address": {
"href": "http://localhost:8080/api/persons/1/address"
}
}
},
{
"address": null,
"name": "Michael",
"_links": {
"self": {
"href": "http://localhost:8080/api/persons/2"
},
"person": {
"href": "http://localhost:8080/api/persons/2{?projection}",
"templated": true
},
"address": {
"href": "http://localhost:8080/api/persons/2/address"
}
}
}
]
},
"_links": {
"self": {
"href": "http://localhost:8080/api/persons"
},
"profile": {
"href": "http://localhost:8080/api/profile/persons"
},
"search": {
"href": "http://localhost:8080/api/persons/search"
}
},
"page": {
"size": 20,
"totalElements": 2,
"totalPages": 1,
"number": 0
}
}
Is there anything wrong with my code? I have tried using old approach without using spring data rest but using rest controller, the same json I posted was working fine. Not sure why spring data rest does not work here.
OK. Seems no way to do this. I have to first post a person by
{"name"="Michael"}
and then post an address by
{"country":"US,"state":"SV:} ,
and last put address to this person
{ "name":"Michael", "address":"localhost:8080/addresses/1" }

Generate links for collection resources for a specific single resource

I wrote a custom controller to handle a GET http://localhost:54000/api/v1/portfolios/{id}/evaluate request.
#RequestMapping(value = "/portfolios/{id}/evaluate", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> evaluate(#PathVariable Long id) {
Portfolio portfolio = portfolioService.evaluate(id);
if (portfolio == null) {
return ResponseEntity.notFound().build();
}
Resource<Portfolio> resource = new Resource<>(portfolio);
resource.add(entityLinks.linkForSingleResource(Portfolio.class, id).withSelfRel());
return ResponseEntity.ok(resource);
}
The current response is
{
"summary" : {
"count" : 24.166666666666668,
"yield" : 0.14921630094043895,
"minBankroll" : -6.090909090909091,
"sharpeRatio" : 0.7120933654645042,
"worstReturn" : -2.4545454545454533,
"losingSeason" : 3,
"return" : 3.6060606060606077
},
"_links" : {
"self" : {
"href" : "http://localhost:54000/api/v1/portfolios/4"
}
}
}
but I would like to add collection resources (summaries and systems) linked to that portfolio:
{
"summary": {
"count": 24.166666666666668,
"yield": 0.14921630094043895,
"minBankroll": -6.090909090909091,
"sharpeRatio": 0.7120933654645042,
"worstReturn": -2.4545454545454533,
"losingSeason": 3,
"return": 3.6060606060606077
},
"_links": {
"self": {
"href": "http://localhost:54000/api/v1/portfolios/4"
},
"portfolio": {
"href": "http://localhost:54000/api/v1/portfolios/4"
},
"summaries": {
"href": "http://localhost:54000/api/v1/portfolios/4/summaries"
},
"systems": {
"href": "http://localhost:54000/api/v1/portfolios/4/systems"
}
}
}
I did not find a way to generate those links with the RepositoryEntityLinks entityLinks object
You can always do something like this:
entityLinks.linkForSingleResource(Portfolio.class, id).slash("systems").withRel("systems");
And if your systems endpoint is implemented in a custom controller method you can use the ControllerLinkBuilder to generate a link to your controller method. Lets say you implemented the getSystems method with id parameter in MyControllerClass - then you can generate the link like this (linkTo and methodOn are static methods in ControllerLinkBuilder):
linkTo(methodOn(MyControllerClass.class).getSystems(id)).withRel("systems");