java orika complex mapping - java

I use Orika and I want map codeActivite1.value from Value.class to tasks.tasks[0].notes of Ligne.class
mapperFactory.classMap(Value.class, Ligne.class).field().field("codeActivite1.value", "tasks.tasks[0].notes").register();
public class Value {
#SerializedName("code_activite1")
private CodeActivite1 codeActivite1;
//getter setter
}
public class CodeActivite1 {
#SerializedName("value")
private String value;
//getter setter
}
public class Ligne {
private Tasks tasks;
//getter setter
}
public class Tasks{
private Task[] tasks;
//getter setter
}
public class Task {
private String notes;
//getter setter
}
ma.glasnost.orika.MappingException: java.lang.IllegalArgumentException: java.lang.String is an unsupported source class for constructing instances of com.xxxx.business.xxxx.bean.Task[]

I solve the problem when I change array by List to Tasks.class
public class Tasks {
private List<Task> tasks;
//getter setter
}
mapperFactory.classMap(Value.class, Ligne.class).field().field("codeActivite1.value", "tasks.tasks[0].notes").register();

Related

JsonIncludeProperties with JsonUnwrapped

Can I use these annotation for my class to my expected json?
public class Staff {
private String name;
private Integer age;
#JsonUnwrapped
private Staff manager;
.... getter and setter ....
}
{
"name": "Fanny",
"age": 24,
"manager": "Timmy"
}
I know I can use JsonIgnoreProperties but I need to unwrap name only. Any solution? Thanks
You can have a getter for the name that returns a String and annotate it with #JsonPropery,
Can I use these annotation for my class to my expected json?
public class Staff {
private String name;
private Integer age;
#JsonIgnore
private Staff manager;
#JsonProperty("managerName")
public String getManagerName() {
return this.manager.getName();
}
.... getter and setter ....
}

I'm not able able access the method of class

I have the class which has to details inside the data http://www.mocky.io/v2/5cacde192f000078003a93bb , i have written a class to get the data and next class to get the details
public class ApiObject {
#SerializedName("status")
#Expose
public String status;
#SerializedName("data")
#Expose
public List<MyData> data = null;
#SerializedName("products")
public List<Products> products = null;
public List<MyData> getData() {
return data;
}
public class MyData{
#SerializedName("details")
public Details details;
#SerializedName("product_count")
public Integer productCount;
public Details getDetails(){
return details;
}
#SerializedName("product_count")
#Expose
private String Product_count;
#SerializedName("products")
public List<Products> getProducts(){
return products;
}
//setter and getters
}
I have created a object of Apiobject in more class
and I'm trying to access the getDetails method
like
ApiObject apiObject ;
apiObject.getData().getDetails();
I'm getting an error cannot resolve a method
getDetails() is a private method. Therefore, can only be accessed from within the class MyData. Make it public for it to be accessible to instances of other classes.
There are two problems in your code:
getDetails() is a private method, make it public if you want to access it.
getData() return a list of MyData objects, not a single instance. So, you should iterate through the list if you want to call the getDetails().
for (MyData data : apiObject.getData()) {
data.getDetails();
}

How to get Entity from RestController depending on mapping URL

I have MyEntity class:
#Entity
#Table("entities)
public class MyEntity {
#ID
private String name;
#Column(name="age")
private int age;
#Column(name="weight")
private int weight;
...getters and setters..
}
In #RestController there are 2 #GetMapping methods.
The first:
#GetMapping
public MyEntity get(){
...
return myEntity;
}
The second:
#GetMapping("url")
public List<MyEntity> getAll(){
...
return entities;
}
It's needed to provide:
1. #GetMapping returns entity as it's described in MyEntity class.
2. #GetMapping("url") returns entities like one of its fields is with #JsonIgnore.
UPDATE:
When I return myEntity, client will get, for example:
{
"name":"Alex",
"age":30,
"weight":70
}
I want in the same time using the same ENTITY have an opportunity depending on the URL send to client:
1.
{
"name":"Alex",
"age":30,
"weight":70
}
2.
{
"name":"Alex",
"age":30
}
You could also use JsonView Annotation which makes it a bit cleaner.
Define views
public class View {
static class Public { }
static class ExtendedPublic extends Public { }
static class Private extends ExtendedPublic { }
}
Entity
#Entity
#Table("entities)
public class MyEntity {
#ID
private String name;
#Column(name="age")
private int age;
#JsonView(View.Private.class)
#Column(name="weight")
private int weight;
...getters and setters..
}
And in your Rest Controller
#JsonView(View.Private.class)
#GetMapping
public MyEntity get(){
...
return myEntity;
}
#JsonView(View.Public.class)
#GetMapping("url")
public List<MyEntity> getAll(){
...
return entities;
}
Already explained here:
https://stackoverflow.com/a/49207551/3005093
You could create two DTO classes, convert your entity to the appropriate DTO class and return it.
public class MyEntity {
private String name;
private int age;
private int weight;
public PersonDetailedDTO toPersonDetailedDTO() {
PersonDetailedDTO person = PersonDetailedDTO();
//...
return person;
}
public PersonDTO toPersonDTO() {
PersonDTO person = PersonDTO();
//...
return person;
}
}
public class PersonDetailedDTO {
private String name;
private int age;
private int weight;
}
public class PersonDTO {
private String name;
private int age;
}
#GetMapping
public PersonDTO get() {
//...
return personService.getPerson().toPersonDTO();
}
#GetMapping("/my_url")
public PersonDetailedDTO get() {
//...
return personService.getPerson().toPersonDetailedDTO();
}
EDIT:
Instead of returning an Entity object, you could serialize it as a Map, where the map keys represent the attribute names. So you can add the values to your map based on the include parameter.
#ResponseBody
public Map<String, Object> getUser(#PathVariable("name") String name, String include) {
User user = service.loadUser(name);
// check the `include` parameter and create a map containing only the required attributes
Map<String, Object> userMap = service.convertUserToMap(user, include);
return userMap;
}
As an example, if you have a Map like this and want
All Details
userMap.put("name", user.getName());
userMap.put("age", user.getAge());
userMap.put("weight", user.getWeight());
Now if You do not want to display weight then you can put only two
parameters
userMap.put("name", user.getName());
userMap.put("age", user.getAge());
Useful Reference 1 2 3

java - MapStruct unable to map fields in base class in mapper function

I have a Base dto and a dto which extends it
public abstract class AbstractItem {
private String upc;
private String quantity;
//getter and setter for upc and quantity
}
public class OrderedItem extends AbstractItem {
private String orderId;
// getter and setter for orderId
}
Then I have a modal which I want to convert from OrderedItem. This modal extends a base modal
public abstract class AbstractModal {
private String qty;
public void setQty(String qty) {
this.qty = qty;
}
public String getQty() {
return qty;
}
}
public class ItemModal extends AbstractModal {
private String orderNbr;
private String upcNbr;
// getter and setter for orderNbr and upcNbr;
}
this is my mapper function using Mapstruct
#Mapper
public interface DtoMapper {
#Mappings({
#Mapping(source = "orderedItem.orderId" target = "orderNbr"),
#Mapping(source = "orderedItem.upc" target = "upcNbr"),
#Mapping(source = "orderedItem.quantity" target = "qty")
})
ItemModal convert(OrderedItem orderedItem);
}
During compilation I get an error Unknown property "qty" in result type test.modal.ItemModal.
Does Mapstruct support mapping fields of base class?
If yes, can you please tell me what is wrong with my code?
I am using java 8 and mapstruct 1.2.0
Edit: added getter and setter for qty in AbstractModal

How to pass a variable from 2 different POJOs?

I got 2 POJOs that are being passed to a HttpEntity and converted to json.
Before passing them i need a variable which is in both of the POJOs with different names because of the needs of the API so i cant change them.
What is the best way without casting and in terms of OOP also within the POJO definition like mentioned in Wikipedia?
Abstract pojo
public abstract class Pojo{
//some common variables
//setter getters
}
PojoOne
public class PojoOne extends Pojo{
private String id;
//setter getter for id
}
PojoTwo
public class PojoTwo extends Pojo{
private String identifier;
// setter getter for identifier
}
Class that
public class SomeOtherClass {
public void getIdForUse(Pojo pojo){
String s = pojo. // How should this be to have ability to get both id and identifier
}
}
Add a common method to the common superclass:
public abstract class Pojo {
public abstract String getId();
// some common variables
// setter getters
}
public class PojoOne extends Pojo {
private String id;
#Override
public String getId() {
return id;
}
//setter for id
}
public class PojoTwo extends Pojo {
private String identifier;
// setter getter for identifier
#Override
public String getId() {
return identifier;
}
}

Categories