I have rest models that I use to build JSON that I send.
A rest model
#Getter #Setter
#ToString
public class OrderRequestModel {
private String orderKeyId;
private String paymentMode;
private double totalAmount;
private List<ProductRequestModel> orderProducts;
private UserDetailsRequestModel seller;
private Date createdAt;
}
The ProductRequestModel is similar
#Getter #Setter
#ToString
public class ProductRequestModel {
private String productKeyId;
private String name;
private double price;
private int qty;
private String imgPath;
private CategoryRequestModel category;
}
I'm passing the models to a DTO layer which is in relation with database (they include a long Id):
#Getter #Setter
#ToString
public class OrderDto implements Serializable {
#Getter(AccessLevel.NONE)
#Setter(AccessLevel.NONE)
private static final long serialVersionUID = 1L;
private Long id;
private String orderKeyId;
private String paymentMode;
private double totalAmount;
private List<ProductDto> orderProducts;
private UserDto seller;
private Date createdAt;
}
And my ProductDto :
#Getter #Setter
#ToString
public class ProductDto implements Serializable {
// ommit this member and do not generate getter / setter
#Getter(AccessLevel.NONE)
#Setter(AccessLevel.NONE)
private static final long serialVersionUID = 1L;
private Long id;
private String productKeyId;
private String name;
private double price;
private int qty;
private String imgPath;
private CategoryDto category = new CategoryDto();
}
When i try to map OrderDto with the associated model i do it implicitelly :
OrderDto orderDto = modelMapper.map(orderRequestModel, OrderDto.class);
In theory, orderKeyId from the model should match with its equivalent in the Dto. Unfortunatelly It returns an error :
Converter org.modelmapper.internal.converter.NumberConverter#3e36f4cc failed to convert java.lang.String to java.lang.Long.
Caused by: org.modelmapper.MappingException: ModelMapper mapping errors:
I do need the Id in the DTO because if i want to make an update I do use "id"
This issue is caused because of the Mapping Strategies. We can set the Mapping Strategies to STRICT so that when mapping from source object property to target object's property it only maps only those property which are perfectly matching in property name as well as it's data type. Below is an example.
public ModelMapper modelMapper() {
ModelMapper modelMapper = new ModelMapper();
modelMapper.getConfiguration()
.setMatchingStrategy(MatchingStrategies.STRICT);
}
FYI:
http://modelmapper.org/user-manual/configuration/#matching-strategies
Related
modelmapper 3.1.0
i have following class:
#Data
public class Outter {
private int id;
private String name;
private List<Inner> innerList;
}
#Data
public class Inner {
private int id;
private String code;
}
i want to convert Outter to OutterDto, and i tried create the OutterDto as follows:
#Data
public class OutterDto {
private String name;
private List<InnerDto> innerList;
#Data
class InnerDto {
private String code;
}
}
the InnerDto is the inner class of OutterDto.
however, it thrown an error:
Failed to instantiate instance of destination example.model.OutterDto$InnerDto. Ensure that example.model.OutterDto$InnerDto has a non-private no-argument constructor.
even i add a constructor public InnterDto() {} for InnerDto.
so does the modelmapper support for inner class?
if it does, how can i do that?
I try to make a simple login with JSF and Managed Beans, but when start the server returns the following error.
WFLYEJB0406: No EJB found with interface of type 'Controlador.UsuarioSessionBean' for binding Controlador.AlmacenVirtualBean/usuarioSession"}
This is the class to save the data...
#ManagedBean
#RequestScoped
public class UsuarioSessionBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
#EJB
private UsuarioSessionDAO usuarioSession;
//private Usuario usuario;
private int usuarioId;
private String nick;
private String pass;
And in other Managed Bean I try to inject the first.
#ManagedBean(name="AlmacenVirtualBean")
#RequestScoped
public class AlmacenVirtualBean {
private AlmacenVirtual almacenVirtual;
private String nombre;
private int usuarioId;
public AlmacenVirtualBean(){}
#EJB
private AlmacenVirtualDAO almacenVirtualDAO;
#ManagedProperty("#{UsuarioSessionBean}")
private UsuarioSessionBean usuarioSession;
That's what I'm doing wrong?
You may get that error, if you change the AlmacenVirtualBean to have:
#EJB
private UsuarioSessionBean usuarioSession;
Your question code can't produce that error.
But you can get usuarioSession=null. You should replace #{UsuarioSessionBean} by #{usuarioSessionBean}.
I'm trying to save a spring entity to Cassandra (2.0.11) using a Cassandra repository. The entity class is as follows:
#Table(value="gs")
public class GsJsonStore implements Serializable {
private static final long serialVersionUID = 7133072282172062535L;
#PrimaryKey
private String id;
#Column(value = "object")
#CassandraType(type=DataType.Name.MAP)
private Map<String, String> _object;
#Column(value = "subject")
private String _subject;
//Getter and Setter methods.
}
The code I'm using to save the object:
GsJsonStore gsJsonStore = new GsJsonStore();
gsJsonStore.setPredicate("Predicate");
gsJsonStore.setSubject("Subject");
gsJsonStore.setObject(jsonToMap(jsonNode));
repository.save(gsJsonStore);
The jsonToMap function:
public Map<String,String> jsonToMap(JsonNode json) {
ObjectMapper mapper = new ObjectMapper();
Map<String, String> result = mapper.convertValue(json, Map.class);
return result;
}
I get the following error while storing the object:
java.lang.IllegalArgumentException:
Value 0 of type class java.util.LinkedHashMap does not correspond to any CQL3 type
Where am I going wrong?
#Indexed
public class Event implements Serializable {
#DocumentId
private Long id;
#Field
#AnalyzerDiscriminator(impl = LanguageDiscriminator.class) // "de", GermanAnalyzer
private String lang;
#IndexedEmbedded
private User user;
}
#Indexed
#Analyzer(impl = GermanAnalyzer.class)
public class User implements Serializable {
#DocumentId
private Long id;
#Field
private String firstName;
}
firstName field will be analyzed in the index User, and will not be analyzed in the index Event.
This is the correct behavior or not?
It will be analyzed in the Event index but the field name will be user.firstName in that case. You can override the default prefix by using the otional attributes of #IndexedEmbedded.
Given an XML file of offerings that is then loaded into a class called Offerings via JAXB.
This class has the following:
Name, Price sub-Class, Modifiers, Ordering Rules etc.
I then create an order and within that order
Order
public class ProductOrder {
private String OrderId;
private Date createDate;
private OrderStatus orderStatus;
private int CustomerOrderID;
private ArrayList<ProductOrderItem> productOrderItems = new ArrayList<ProductOrderItem>();
}
Order Item
public class ProductOrderItem {
private int OrderItemID;
private **Offering** offering;
private Map<String, Integer> qtylist = new HashMap<String, Integer>();
private ArrayList<Modifier> modifiers = new ArrayList<Modifier>();
private int qty;
}
Offering
#XmlRootElement(name = "offering")
#XmlAccessorType(XmlAccessType.FIELD) // NONE)
public class Offering {
#XmlAttribute
private String id;
#XmlElement
private String offeringName;
#XmlElement
private String description;
#XmlElement
private Integer price;
}
The Offering and Modifiers are classes with JAXB already which I only want to push part of the XML. How would I change the anotations such that only part of the elements are sent? For example not the offering -> modifiers?
Use #XmlTransient instead of the #XmlElement tag.