简体   繁体   中英

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

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": "sita@gmail.com",
       "January": 3,
       "February": 10,
       "March": 1,
       "April": 3,
       "May": 32,
       "June": 31
   },
   {
       "userName": "Gita",
       "userEmail": "gita@gmail.com",
       "January": 3,
       "February": 10,
       "March": 1,
       "April": 3,
       "May": 32,
       "June": 31
   },
   {
       "userName": "Ram",
       "userEmail": "ram@gmail.com",
       "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.

If we keep like this, how do I map it to the specific month in a serviceImp? Can please provide the code? How do I map it. Like If I got the JSONObject like this -

for (int i = 0; i < filterUser.length(); i++) { 
    JSONObject jsb = (JSONObject) filterUser.get(i); 
    jsb.keySet().forEach(keyStr -> { 
         // Here Key is January, and jsb.get(keyStr) -> January data 
         // How can I set the setter and getter here ? 
    // }); 
    //  filteredTotalFeedbackData.put(i, jsb); 
}

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": "sita@gmail.com",
       "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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM