I have two classes, one of them is a map with name and position of a tabbed file:
public enum fieldPosition {
field_1(0),
field_2(1),
// other fields
}
The other is a simple entity:
public class simpleEntiy {
private String name;
private String desc;
// other attributes
}
I want to generate a tabbed file based on position of enum class, but using the object from entity class with spring annotations. Its possible?
Is there some way to use map on entity class to mapper the enum class?
For example:
public class simpleEntiy {
//some annotation to field_1 or position 0
private String name;
//some annotation to field_2 or position 1
private String desc;
// other attributes
}
I'm using java 8 and spring boot 1.4.3.
Thanks
Related
Suppose I have class A like this
class A {
private String Id;
private String name;
private String age;
//... some other hundres of feilds
}
How can I create another DTO named CreateA which contains all fields for A except the one I mention like id ?
something like this
#sameAs("A")
#skip("Id")
class CreateA {}
is there any java library for this ?
I want to sort List using annotation. so is there any annotation so i can sort my list by date? or is there any possibility to create custom annotation ?
Note: I am not using jpa or hibernate things. i am using bright-spot (DARI framework )
My code looks like.
public class Student {
private String name;
private Date dob;
}
My Second class
public class School {
private String name;
private List<Student> students; // here i want to sort list
}
Does the documented ToolUi.DefaultSortField do what you want?
#ToolUi.DefaultSortField("dob")
public class Student extends Content {
private String name;
#Indexed
private Date dob;
}
See http://docs.brightspot.com/cms/developers-guide/content-modeling/content-modeling-annotations.html#toolui-defaultsortfield
Need some help here! I have a Java Rest API which is getting data from a .net endpoint and passing it on to the UI. The JSON properties are in capital case and I want to convert them in JAVA before sending it to the UI. Any pointers on this?
In java, I have a class like below:
public class Person {
#JsonProperty("Name")
private String name;
#JsonProperty("Age")
private int age;
}
I am using #JsonProperty as keys in .net are starting with capitalCase. How can I convert this back before sending it to the UI in Java?
Thanks for the help!
Create another class with the same structure and use there other names that you want. Something like this:
// Class to read .NET object
public class Person {
#JsonProperty("Name")
private String name;
#JsonProperty("Age")
private int age;
}
// Class to represent the object in Java REST API
public class Person {
#JsonProperty("name")
private String name;
#JsonProperty("age")
private int age;
}
// Class to represent the object in Java REST API,
// in case you use some standard library that
// uses property names for JSON as is
public class Person {
private String name;
private int age;
}
Of course you should put these classes into different packages.
Your code can look as follows:
xxx.dotnet.Person dotnetPerson = doSomethingViaDotNet(...);
yyy.rest.Person restPerson = new yyy.rest.Person();
restPerson.setName(dotnetPerson.getName());
restPerson.setAge(dotnetPerson.getAge());
...
return restPerson;
If you decide to use MapStruct, your code may looks as follows:
#Mapper
public interface PersonMapper {
PersonMapper INSTANCE = Mappers.getMapper( PersonMapper.class );
yyy.rest.Person dotnetToRest(xxx.dotnet.Person dotnetPerson);
}
Since all attributes have the same names and types you don't need anything else in your mapper.
MapStruct will generate a class that implements this interface. Usage will be as follows:
restPerson = PersonMapper.INSTANCE.dotnetToRest(dotnetPerson);
I have a domain javabean, some bean hvae a lot of information with password
and login Ip, I use the #jsonIgnore to filter that property which I dont
want the end user know.
But there has a problem,In other method
I use the same javabean to send back
to front side,but now I need some property from this domain
has anyway can cancel this #jsonIgnore in some specific method?
#JsonIgnore
private String address;
private Integer drawnum;
but now I need address , I cant do this.....
I dont want to use the for loop to add in other object.
I think that what you are looking for is the concept of JsonView : in some cases you want a set of attributes to be serialized, and in some other cases you want a (slightly) different set of attributes to be serialized.
Check this excellent tutorial, it explains evrything, even the use with Spring MVC.
Create classes to annotate the fields :
public class Views {
public static class Public {
}
public static class Internal extends Public {
}
}
Annotate the fields :
public class Item {
#JsonView(Views.Public.class)
public int id;
#JsonView(Views.Public.class)
public int drawnum;
#JsonView(Views.Internal.class)
public String address;
}
In the controller, if you want only "public" properties to be serialized ;
#JsonView(Views.Public.class)
#RequestMapping("/items/{id}")
public Item publicItem(#PathVariable int id) {
Result : {"id":2,"drawnum":5}
In another controller, if you want all properties to be serialized ;
#JsonView(Views.Internal.class)
#RequestMapping("/items/{id}")
public Item internalItem(#PathVariable int id) {
Result : {"id":2,"drawnum":5,"address":"My address"}
I'm using neo4j + spring data. To access the data I'm using interfaces, that extends GraphRepository<E>. For example
public interface EntryRepository extends GraphRepository<Entry> {
#Query("start parent=node({0}), entry=node({1}) "
+ "match parent-[*1..2{removed:false}]->entry "
+ "return distinct entry")
Entry findOne(Long parentId, Long entryId);
}
I'm trying to get data, that differs from my domain models. My custom models looks like that
#QueryResult
public class EntryBean {
#ResultColumn("id")
private Long id;
#ResultColumn("name")
private String name;
#ResultColumn("content")
private String content;
...
//getters and setters
}
#QueryResult
public class BoardBean {
#ResultColumn("id")
private Long id;
#ResultColumn("name")
private String name;
...
//getters and setters
}
Obviously, that it will be better to separate duplicate fields to Base class and inherit from it. So, i'm doing next steps
#QueryResult
public class BaseBean {
#ResultColumn("id")
private Long id;
#ResultColumn("name")
private String name;
...
}
#QueryResult
public class EntryBean extends BaseBean{
#ResultColumn("content")
private String content;
...
//getters and setters
}
And I don't need BoardBean anymore. But when I'm trying run query
public interface EntryRepository extends GraphRepository<Entry> {
#Query("start user=node({0}), board=node({1}) "
+ "... "
+ "return id(entry) as id, entry.name as name, entry.content as content")
List<EntryBean> getRelatedEntries(Long userId, Long boardId);
}
I get filled by data just fields that directly declared into EntryBean class (i. e. "content" field).
So, How I can correctly implement the #QueryResult class hierarcy?
This is a bug which has been present for almost two years (even in 2.3.5.RELEASE!) in the class in charge of converting annotated POJOs.
Indeed, it calls getDeclaredFields on the most concrete type thus skipping possibly inherited annotated fields.
Before the issue is fixed, my piece of advice would be to tolerate this superficial field duplication on your side and not relying on inheritance for now.