How to persist nested pojo objects in Java? - java

I have class Rowdata
public class FileData {
Map<Long, List<String>> cacheData = new LinkedHashMap<Long, List<String>>();
List<String> header = new ArrayList<String>();
public Map<Long, List<String>> getCacheData() {
return Collections.unmodifiableMap(cacheData);
}
public List<String> getHeaderMapping() throws Exception {
return header;
}
public void putData(long key, List<String> row) {
cacheData.put(key, row);
}
public int size() {
return cacheData.size();
}
public void setHeader(List<String> header) {
this.header = header;
}
}
I would like persist this object. What is the best way to persist? Shall I proceed with RDBMS? then what is the table schema?
Or Shall I proceed with NoSQL? then We already have CouchDB in our project . Please advice on this.

Related

Jackson searches method on wrong level on deserialization

I am trying to read a JSON into the class. Jackson wants to apply a field of a subelement to the element itself, where it of course does not exist.
This is the JSON:
{
"authorizationRequest":{
"scope":["write","read"],
"resourceIds":["metadata"],
"approved":true,
"authorities":[],
"authorizationParameters":{
"scope":"write read",
"response_type":"token",
"redirect_uri":"",
"state":"",
"stateful":"false",
"client_id":"5102686_metadata"
},
"approvalParameters":{},
"state":"",
"clientId":"5102686_metadata",
"redirectUri":"",
"responseTypes":["token"],
"denied":false
},
"credentials":"",
"clientOnly":false,
"name":"testuser"
}
The classes look like the following:
// The main class that I try do deserialize:
public class DeserializedOAuth2Authentication extends OAuth2Authentication{
private String name;
private boolean clientOnly;
private AuthorizationRequest authorizationRequest = new DefaultAuthorizationRequest("", new ArrayList<>());
public DeserializedOAuth2Authentication() {
super(new DefaultAuthorizationRequest("", new ArrayList<>()), null);
}
#Override
#JsonProperty
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
#Override
#JsonProperty
public boolean isClientOnly() {
return clientOnly;
}
public void setClientOnly(boolean clientOnly) {
this.clientOnly = clientOnly;
}
#Override
#JsonProperty
public AuthorizationRequest getAuthorizationRequest() {
return authorizationRequest;
}
public void setAuthorizationRequest(AuthorizationRequest authorizationRequest) {
this.authorizationRequest = authorizationRequest;
}
}
AuthorizationRequest is an interface with all the getters for the listed elements; it is configured to be serialized by a DefaultAuthorizationRequest class also containing the respective setters and implementing fileds with corresponding names.
public class DefaultAuthorizationRequest implements AuthorizationRequest, Serializable {
private Set<String> scope = new LinkedHashSet<String>();
private Set<String> resourceIds = new HashSet<String>();
private boolean approved = false;
private Collection<GrantedAuthority> authorities = new HashSet<GrantedAuthority>();
private Map<String, String> authorizationParameters = new ConcurrentHashMap<String, String>();
private Map<String, String> approvalParameters = new HashMap<String, String>();
private String resolvedRedirectUri;
public Map<String, String> getAuthorizationParameters() {
return Collections.unmodifiableMap(authorizationParameters);
}
public Map<String, String> getApprovalParameters() {
return Collections.unmodifiableMap(approvalParameters);
}
public String getClientId() {
return authorizationParameters.get(CLIENT_ID);
}
public Set<String> getScope() {
return Collections.unmodifiableSet(this.scope);
}
public Set<String> getResourceIds() {
return Collections.unmodifiableSet(resourceIds);
}
public Collection<GrantedAuthority> getAuthorities() {
return Collections.unmodifiableSet((Set<? extends GrantedAuthority>) authorities);
}
public boolean isApproved() {
return approved;
}
public boolean isDenied() {
return !approved;
}
public String getState() {
return authorizationParameters.get(STATE);
}
public String getRedirectUri() {
return resolvedRedirectUri == null ? authorizationParameters.get(REDIRECT_URI) : resolvedRedirectUri;
}
public Set<String> getResponseTypes() {
return OAuth2Utils.parseParameterList(authorizationParameters.get(RESPONSE_TYPE));
}
public void setRedirectUri(String redirectUri) {
this.resolvedRedirectUri = redirectUri;
}
public void setScope(Set<String> scope) {
this.scope = scope == null ? new LinkedHashSet<String>() : new LinkedHashSet<String>(scope);
authorizationParameters.put(SCOPE, OAuth2Utils.formatParameterList(scope));
}
public void setResourceIds(Set<String> resourceIds) {
this.resourceIds = resourceIds == null ? new HashSet<String>() : new HashSet<String>(resourceIds);
}
public void setApproved(boolean approved) {
this.approved = approved;
}
public void setAuthorities(Collection<? extends GrantedAuthority> authorities) {
this.authorities = authorities == null ? new HashSet<GrantedAuthority>() : new HashSet<GrantedAuthority>(
authorities);
}
public void setAuthorizationParameters(Map<String, String> authorizationParameters) {
String clientId = getClientId();
Set<String> scope = getScope();
this.authorizationParameters = authorizationParameters == null ? new HashMap<String, String>()
: new HashMap<String, String>(authorizationParameters);
}
public void setApprovalParameters(Map<String, String> approvalParameters) {
this.approvalParameters = approvalParameters == null ? new HashMap<String, String>()
: new HashMap<String, String>(approvalParameters);
}
....
}
On calling read on the above JSON string I get an exception
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "scope" (class de.mvbonline.vlx.auth.oauth2.DeserializedOAuth2Authentication), not marked as ignorable (3 known properties: "name", "authorizationRequest", "clientOnly"])
at [Source: (String)"{ "credentials":"", "clientOnly":false, "authorizationRequest":{ "scope":["write","read"], "resourceIds":["metadata"], "approved":true, "authorities":[], "authorizationParameters":{ "scope":"write read", "response_type":"token", "redirect_uri":"", "state":"", "stateful":"false", "[truncated 316 chars]; line: 1, column: 111] (through reference chain: de.mvbonline.vlx.auth.oauth2.DeserializedOAuth2Authentication["scope"])
Of course the field "scope" is not in the context of DeserializedOAuth2Authentication, but in the context of DefaultAuthorizationRequest. Why is Jackson searching in the wrong class for it?
I am unsing Jackson version 2.12.4
Make sure that DefaultAuthorizationRequest can be serialized and deserialized by Jackson. I guess that they are not for several reasons. Two that I can think of:
You have to let Jackson know how to deserialize DefaultAuthorizationRequest class. One possible solution would be to add a #JsonCreator and #JsonProperty to the class. The same applies to GrantedAuthority class.
DefaultAuthorizationRequest has fields of type Map, which need special attention. See these links on how to convert a JSON String to a Map<String, String> or, if the Map has custom objects, how to deserialize into a HashMap of custom objects
Also, you can take a look at Map Serialization and Deserialization with Jackson
I found my problem.
I formerly mapped my concrete implementation of the interface AuthorizationRequest via a handler:
mapper.addHandler(new DeserializationProblemHandler() {
#Override
public Object handleMissingInstantiator(DeserializationContext ctxt, Class<?> instClass, ValueInstantiator valueInsta, JsonParser p, String msg) throws IOException {
if(instClass.isAssignableFrom(AuthorizationRequest.class)) {
return new DeserializedAuthorizationRequest();
}
return super.handleMissingInstantiator(ctxt, instClass, valueInsta, p, msg);
}
});
This seems to be definitely not the same as annotating the field with the concrete class. This now works without problems:
public class DeserializedOAuth2Authentication extends OAuth2Authentication{
...
#Override
#JsonProperty("authorizationRequest")
#JsonDeserialize(as = DeserializedAuthorizationRequest.class)
public AuthorizationRequest getAuthorizationRequest() {
return authorizationRequest;
}
public void setAuthorizationRequest(AuthorizationRequest authorizationRequest) {
this.authorizationRequest = authorizationRequest;
}
}

Map String Array into a POJO with Dozer

I'm trying to convert a List<String[]> into List<Object> using Dozer but unable to map the index values to the property fields using mapper API configuration.
How can I map the members of the String[] into individual object fields with each index targeting a specific field? (e.g. [0] -> name, and [1] -> role)
DozerBeanMapper mapper = new DozerBeanMapper();
BeanMappingBuilder builder = new BeanMappingBuilder() {
#Override
protected void configure() {
mapping(String[].class, User.class)
.fields(this_(), "name"); // HOW do I specify index?**
}
};
mapper.addMapping(builder);
List<String[]> users = new ArrayList<>();
String[] user1 = {"Jill", "SDE"};
String[] user2 = {"Jack", "PM"};
users.add(user1);
users.add(user2);
List<User> userList = mapObjects(mapper, users, User.class);
where mapObjects() is;
private static <T1, T2> List<T2> mapObjects(DozerBeanMapper mapper, List<T1> sourceList, Class<T2> destinationClazz) {
try {
return sourceList.stream()
.map(i -> mapper.map(i, destinationClazz))
.collect(Collectors.toList());
} catch (Exception e) {
...
}
return new ArrayList<>();
}
and User class;
class User {
String name;
String role;
// getter & setter
}
It worked perfectly with the following configuration;
DozerBeanMapper mapper = new DozerBeanMapper();
BeanMappingBuilder builder = new BeanMappingBuilder() {
#Override
protected void configure() {
mapping(String[].class, User.class)
.fields(this_(), "name", FieldsMappingOptions.customConverterId("arrToName"))
.fields(this_(), "role", FieldsMappingOptions.customConverterId("arrToRole"));
}
};
final Map<String, CustomConverter> customConverterMap = new HashMap<>();
customConverterMap.put("arrToName", new ArrToNameConverter());
customConverterMap.put("arrToRole", new ArrToRoleConverter());
mapper.setCustomConvertersWithId(customConverterMap);
mapper.addMapping(builder);
Utilizing a logic where String[] is mapped into name and role fields separately via custom converters, which are targeting a specific index of the input String[]. With dozer, you can essentially define custom converters and assign them an id, and refer them with those ids inside of field mappings FieldsMappingOptions.customConverterId("{id}")
where ArrToNameConverter;
public class ArrToNameConverter extends DozerConverter<String[], String> {
public ArrToNameConverter() {
super(String[].class, String.class);
}
#Override
public String convertTo(String[] strings, String user) {
return strings[0];
}
#Override
public String[] convertFrom(String user, String[] strings) {
return new String[0];
}
}
and ArrToRoleConverter;
public class ArrToRoleConverter extends DozerConverter<String[], String> {
public ArrToRoleConverter() {
super(String[].class, String.class);
}
#Override
public String convertTo(String[] strings, String user) {
return strings[1];
}
#Override
public String[] convertFrom(String user, String[] strings) {
return new String[0];
}
}
With the above mapper, I was able to get the following result;
[User(name=Jill, role=SDE), User(name=Jack, role=PM)]

arraylist/list RESTful web service json

Hello I want to ask about web service, how to show values from database to Map<>?
here is my code
#GET
#Path("/jurusan/{kode_jurusan}")
#Produces(MediaType.APPLICATION_JSON)
public Map getMatkulByjurusan(#PathParam("kode_jurusan") String kode_jurusan){
Map<String, Object> rs = new HashMap<String, Object>();
rs.put("Pesan", "Berhasil");
System.out.println("Jurusan "+kode_jurusan);
try {
createConnection();
MyMap matkul =(MyMap) jt.queryObject("select matkul from tb_matkul where kode_jurusan = ?", new Object[] {kode_jurusan}, new MyMap());
closeConnection();
if(matkul != null){
rs.put("result", matkul);
}
} catch (Exception e) {
rs.put("Pesan", "Gagal karena : " +e.getMessage());
}
return rs;
}
}
but when I try to acess http://localhost:9090/Service/matkul/jurusan/40 I get the following message:
{"Pesan":"Gagal karena : Incorrect result size: expected 1, actual 14"}
this MyMap class
public class MyMap implements Serializable, RowMapper{
private static final long serialVersionUID = -8840406844877458198L;
public HashMap<String, Object> map = new HashMap<String, Object>();
public HashMap<String, Object> getMap() {
return map;
}
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
MyMap dto=new MyMap();
int rowCount = rs.getMetaData().getColumnCount();
for (int i = 1; i <= rowCount; i++) {
dto.map.put(rs.getMetaData().getColumnLabel(i), rs.getObject(i));
}
return dto;
}
public void put(String name, Object o){
map.put(name, o);
}
public Object get(String name){
return map.get(name);
}
public String getString(String name){
return (String)map.get(name);
}
public Integer getInt(String name){
return (Integer)map.get(name);
}
public Date getDate(String name){
return (Date)map.get(name);
}
public BigDecimal getBigDecimal(String name){
return (BigDecimal)map.get(name);
}
}
Use queryForList method instead queryObject.
you can find an example to map multiple rows to list using jdbcTemplate from here .
Looks like the problem is on the database query, as the exception says, the query is expecting only 1 row as result and it produces 14.
Moreover, depending on which framework are you using you should probably provide a way to serialize the MyMap class

Java - Remove multiple IFs for null check

I get an object and a map in the method, and I need to migrate all object field values to a map. Which will be later saved in the DB. Values of the map cannot be null.
This is the code:
public static final EMP_LAST_NAME_ATTR = "firstName";
public static final EMP_FIRST_NAME_ATTR = "lastName";
...ect.
and
public void addAttributes(Map<String, String> attributes, Employee employee) {
if (StringUtils.isNotBlank(employee.getFirstName())) {
attributes.put(EMP_FIRST_NAME_ATTR, employee.getFirstName());
}
if (StringUtils.isNotBlank(employee.getLastName())) {
attributes.put(EMP_LAST_NAME_ATTR, employee.getLastName());
}
if (StringUtils.isNotBlank(employee.getEmail())) {
attributes.put(EMP_EMAIL_ATTR, employee.getEmail());
}
...etc many more ifs
}
Unfortunately it has to be a map, as the DB table is created as key/value, and I can't change the the entities.
Any way of shortening this IF nightmare?
One way would be to refactor the if block into its own method:
private void putIfNotBlank(Map<String, String> attributes, String key, String value) {
if (StringUtils.isNotBlank(value)) {
attributes.put(key, values);
}
}
and your method beomes easier to read:
public void addAttributes(Map<String, String> attributes, Employee employee) {
putIfNotBlank(attributes, EMP_FIRST_NAME_ATTR, employee.getFirstName());
putIfNotBlank(attributes, EMP_LAST_NAME_ATTR, employee.getLastName());
putIfNotBlank(attributes, EMP_EMAIL_ATTR, employee.getEmail());
}
private void addAttribute(Map<String, String> attributes, String key, String value) {
if (StringUtils.isNotBlank(value)) {
attributes.put(key, value);
}
}
public void addAttributes(Map<String, String> attributes, Employee employee) {
addAttribute(attributes, EMP_FIRST_NAME_ATTR, employee.getFirstName());
addAttribute(attributes, EMP_LAST_NAME_ATTR, employee.getLastName());
addAttribute(attributes, EMP_EMAIL_ATTR, employee.getEmail());
}
Make a method to do it for you.
public void addAttributes(Map<String,String> attributes, Employee employee)
{
addAttribute(attributes, EMP_FIRST_NAME_ATTR, employee.getFirstName());
addAttribute(attributes, EMP_LAST_NAME_ATTR, employee.getLastName());
.....
}
private void addAttribute(Map<String,String> attributes, String ATTR_NAME, String value)
{
if(StringUtils.isNotBlank(value)) attributes.put(ATTR_NAME, value);
}
Problem solved.
Do some refactoring by creating a utility method that captures the essence of the check:
public static void putIfNotBlank(Map<String, String> map, String key, String value) {
if (StringUtils.isNotBlank(value))
map.put(key, value);
}
Then call it for each attribute:
putIfNotBlank(attributes, EMP_EMAIL_ATTR, employee.getEmail());
Use Jackson Json ObjectMapper library
ObjectMapper mapper = new ObjectMapper();
Map<String,String> map = mapper.convert(mapper.writeValueAsString(<yourObject),new TypeReference<HashMap<String,String>>(){});
You could define a new Map that had this behaviour built in:
public class NonBlankValueMap extends HashMap<String, String> {
#Override
public String put(String key, String value) {
if (StringUtils.isNotBlank(value))
return super.put(key, value);
return null;
}
}
Then simply:
Map<String, String> attributes = new NonBlankValueMap();
And:
attributes.put(EMP_FIRST_NAME_ATTR, employee.getFirstName());

Bind multiple elements to map with attribute as key with java XML annotations, JAXB

I have an XML source from which I unmarshall Objects with JAXB.
The XML source:
<album>
<name>something</name>
<id>003030</id>
<artist>someone</artist>
...
</album>
The java source is like (with the required getter/setters as well):
#XmlRootElement(name="album")
class Album {
String name;
Long id;
String artist;
...
}
So far so good. Now I get some image urls in different sizes within album list:
...
<image size="small">http://.../small.jpg</image>
<image size="medium">http://.../medium.jpg</image>
<image size="large">http://.../large.jpg</image>
...
I want to map it to a java Map something like this:
Map<String,String> imageUrls;
Where the map's key would be the size attribute and the map's value would be the element value.
If it's possible, how should I annotate this variable?
helper class Pair
#XmlAccessorType(XmlAccessType.FIELD)
public class Pair {
#XmlAttribute
private String key;
#XmlValue
private String value;
public Pair() {
}
public Pair(String key, String value) {
this.key = key;
this.value = value;
}
//... getters, setters
}
List of pairs
#XmlAccessorType(XmlAccessType.FIELD)
public class PairList
{
private List<Pair> values = new ArrayList<Pair>();
public PairList() {
}
//...
}
adaptor
public class MapAdaptor extends XmlAdapter<PairList, Map<String, String>>
{
#Override
public Map<String, String> unmarshal(PairList list) throws Exception
{
Map<String, String> retVal = new HashMap<String, String>();
for (Pair keyValue : list.getValues())
{
retVal.put(keyValue.getKey(), keyValue.getValue());
}
return retVal;
}
#Override
public PairList marshal(Map<String, String> map) throws Exception
{
PairList retVal = new PairList();
for (String key : map.keySet())
{
retVal.getValues().add(new Pair(key, map.get(key)));
}
return retVal;
}
}
usage in your entity
#XmlJavaTypeAdapter(value = MapAdaptor.class)
private Map<String, String> imageUrls = new HashMap<String, String>();
PS
You can do it without class PairList using Pair[] instead of PairList
adaptor
public class MapAdaptor extends XmlAdapter<Pair[], Map<String, String>>
{
#Override
public Map<String, String> unmarshal(Pair[] list) throws Exception
{
Map<String, String> retVal = new HashMap<String, String>();
for (Pair keyValue : Arrays.asList(list))
{
retVal.put(keyValue.getKey(), keyValue.getValue());
}
return retVal;
}
#Override
public Pair[] marshal(Map<String, String> map) throws Exception
{
List<Pair> retVal = new ArrayList<Pair>();
for (String key : map.keySet())
{
retVal.add(new Pair(key, map.get(key)));
}
return retVal.toArray(new Pair[]{});
}
}
but in this case you can't control name of every pair. It will be item and you can't change it
<item key="key2">valu2</item>
<item key="key1">valu1</item>
PS2
If you will try use List<Pair> instead of PairList, you will get Exception
ERROR: java.util.List haven't no-arg constructor

Categories

Resources