Create Project: springboot_entity_constructor (add Spring Boot Starters from the table)
Create Package: entities (inside main package)
– Create Class: PersonEntity.java (inside package entities)
Create Package: controllers (inside main package)
– Create Class: MyController.java (inside package controllers)
PersonEntity.java
package com.ivoronline.springboot_entity_constructor.entities;
public class PersonEntity {
//PROPERTIES
private long id;
private String name;
private Integer age;
//CONSTRUCTOR
public PersonEntity(long Id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
//GETTERS
public long getId () { return id; }
public String getName() { return name; }
public Integer getAge () { return age; }
}
MyController.java
package com.ivoronline.springboot_entity_constructor.controllers;
import com.ivoronline.springboot_entity_constructor.entities.PersonEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class MyController {
@ResponseBody
@RequestMapping("/Hello")
public String hello() {
PersonEntity personEntity = new PersonEntity(1, "John", 20);
return "Hello " + personEntity.getName();
}
}