How to create a POJO?
A POJO is just a plain, old Java Bean with the restrictions removed. Java Beans must meet the following requirements:
- Default no-arg constructor
- Follow the Bean convention of getFoo (or isFoo for booleans) and setFoo methods for a mutable attribute named foo; leave off the setFoo if foo is immutable.
- Must implement java.io.Serializable
POJO does not mandate any of these. It's just what the name says: an object that compiles under JDK can be considered a Plain Old Java Object. No app server, no base classes, no interfaces required to use.
The acronym POJO was a reaction against EJB 2.0, which required several interfaces, extended base classes, and lots of methods just to do simple things. Some people, Rod Johnson and Martin Fowler among them, rebelled against the complexity and sought a way to implement enterprise scale solutions without having to write EJBs.
Martin Fowler coined a new acronym.
Rod Johnson wrote "J2EE Without EJBs", wrote Spring, influenced EJB enough so version 3.1 looks a great deal like Spring and Hibernate, and got a sweet IPO from VMWare out of it.
Here's an example that you can wrap your head around:
public class MyFirstPojo
{
private String name;
public static void main(String [] args)
{
for (String arg : args)
{
MyFirstPojo pojo = new MyFirstPojo(arg); // Here's how you create a POJO
System.out.println(pojo);
}
}
public MyFirstPojo(String name)
{
this.name = name;
}
public String getName() { return this.name; }
public String toString() { return this.name; }
}
How would I create a POJO class for such a data structure?
First create a Product pojo :
class Product {
private String productName;
…
…
// getters / setters
}
then your ProductList class could have a list of Products instead :
class ProductList {
private boolean availability;
private List<Product> our_productList;
...
//Getters & setters
}
Creating a POJO from JSON and retrieving the properties using the POJO classes
You are mixing GSON annotations @SerializedName
and @Expose
with Jackson ObjectMapper
. You should use @JsonProperty
annotation instead as follows:
@Data
public class PickEvent {
@JsonProperty("pickEventActivities")
private List<PickEventActivity> mPickEventActivities;
}
You must do the same for all the other classes.
Generic way to create different pojo objects and persist in db
If you have resources which use different repositories maybe you should have different REST endpoints (or whatever you are using) and service classes right.
One simple way to get rid of your if-else blocks would be to use a Map
Map<String, Consumer<?>> map = new HashMap<>();
map.put("Teacher", (e) -> teacherRepository.save(mapper.readValue(e,Teacher.class))
});
and you can use it like,
map.get("Teacher").accept(jsonString);
Related Topics
Difference Between Jar and War in Java
Clone() VS Copy Constructor VS Factory Method
Maven - Always Download Sources and Javadocs
Initialising Mock Objects - Mockito
How to Make My String Comparison Case-Insensitive
How to Populate a Listview in Javafx Using Custom Objects
Nice Looking Progress Bar in Java
How to Create Table Using Ascii in a Console
Using Mockito with Multiple Calls to the Same Method with the Same Arguments
How to Add Hyperlink in Jlabel
How to Run Testng from Command Line
Spring: How to Inject an Httpservletrequest into a Request-Scoped Bean
Difference Between @JSONignore and @JSONbackreference, @JSONmanagedreference
Password Protected Zip File in Java
Parsing Iso 8601 Date Format Like 2015-06-27T13:16:37.363Z in Java