Home > Blockchain >  Spring boot - How to Write Setter and Getter field Name Dynamic way
Spring boot - How to Write Setter and Getter field Name Dynamic way

Time:07-31

I am getting the record last 6 months from the database. Based on the current Date. I am storing into an JSONArray when I am returning it - I am getting Serialization Error, because there is not Getter and setter. My data looks like this -

[
   {
       "userName": "Sita",
       "userEmail": "[email protected]",
       "January": 3,
       "February": 10,
       "March": 1,
       "April": 3,
       "May": 32,
       "June": 31
   },
   {
       "userName": "Gita",
       "userEmail": "[email protected]",
       "January": 3,
       "February": 10,
       "March": 1,
       "April": 3,
       "May": 32,
       "June": 31
   },
   {
       "userName": "Ram",
       "userEmail": "[email protected]",
       "January": 3,
       "February": 10,
       "March": 1,
       "April": 3,
       "May": 32,
       "June": 31
   }
]

Now the question is --> Month Name can come dynamiclly. How I can I set this into my setter Getter Method, and call it from ServiceImp --> controller

I can't do like this -

@Data
@NoArgsConstructor
@AllArgsConstructor

private String userName;
   private String userName;
   private String January;
   private String February;
   private String March;
   private String April;
   private String May;
   private String June;
   private String July;
   private String August;
   private String September;
   private String October;
   private String November;
   private String December;

Its not the good way. I tried with many way. Can you please help me how I can achive this, and Write In a serviceImpl and Controller file. I am new to this spring boot, Any help would be appriciated.

CodePudding user response:

There are few things which I am assuming, because as per your details the question is somewhat unclear.

You have a database having records as per what you have mentioned in your question.

[
   {
       "userName": "Sita",
       "userEmail": "[email protected]",
       "January": 3,
       "February": 10,
       "March": 1,
       "April": 3,
       "May": 32,
       "June": 31
   }
]

Its basically Json Object contained inside Json Array, So, as per Java you will have a Entity class to hold the values and that Entity class will be contained inside a List.

@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
class Details{
    private String userName;
    private String January;
    private String February;
    private String March;
    private String April;
    private String May;
    private String June;
    private String July;
    private String August;
    private String September;
    private String October;
    private String November;
    private String December;
}

Your result after fetching the data from database will be something like,

List<Details> result = //database call (Repository call);

Now, this all things will be happening inside some service class , we are calling that service class as DetailsServiceImpl.java

Repository

@Repository
interface DetailsRepository extends JpaRepository<Details,Long>{
    
}

Service

@Service
@RequiredArgsConstructor
class DetailsServiceImpl{
    private final DetailsRepository detailsRepository;
    
    public List<DetailsDto> getLastSixMonthsData(){
        //List<Details> result = //detailsRepository database call;

        //either use Modelmapper to copy the data from entity to DTO
        //BeanUtils can also be used.
        return //Copied ArrayList<DetailsDto>(); which will be populated by ModelMapper or BeanUtils.
    }
}

DTO This DTO is responsible for rendering the data to controller, or you can say sending the representable data to controller.

@Data
//@JsonInclude(JsonInclude.Include.NON_NULL) can be used if you dont want non-null values in your response.
class DetailsDto{
    private String userName;
    private String January;
    private String February;
    private String March;
    private String April;
    private String May;
    private String June;
    private String July;
    private String August;
    private String September;
    private String October;
    private String November;
    private String December;
}

RestController

@RestController
@RequiredArgsConstructor
class DetailsController{
    private final DetailsServiceImpl detailsService;
    public ResponseEntity<List<DetailsDto>> getDetailsLastSixMonths(){
        return new ResponseEntity<List<DetailsDto>>(detailsService.getLastSixMonthsData(), HttpStatus.OK);
    }
}

Links to follow How to use BeanUtils.copyProperties?

Copy one class properties to another class, with set the field to map in java Spring boot

No need to use JSONObject and operating on each object. Hope it helps.

  • Related