lundi 23 mai 2016

Is it bad when create every object class for every Rest's Request and Response body?

I'm working in a Spring Boot project, as my implement at the moment, almost for each API i have Request and Response class.

For example :

    @RequestMapping(value = "/notice",method = RequestMethod.POST)
    public AddNoticeResponse addNotice(@Valid @RequestBody AddNoticeRequest){
      Notice notice = ...// creating new notice from AddNoticeRequest
      noticeRepository.save(notice);
      AddNoticeResponse response = ...// creating new response instance from Notice
    return response;
    } 

And request,response class below :

@Data
@AllArgsConstructor
public class AddNoticeRequest{
      private String subject;
      private String message;
      private Long timeToLive;

}

//sorry i'll ignore some annotations from now
public class AddNoticeResponse{
       private String subject;
       private String message;
       private Long timeToLive;
       private Date createTime;
       private String creator;
} 

So i have two problems, first is : creating too much classes and naming them some times made me nuts. And second is : If some request and response have common fields. For example: There's two kind of Notice : Email and Notification. Email:

public class Email{
     private String subject;
     private String message;
     private String receiver;
     }

So , should i use inner class extends the common class or just put all the fields into one class? Which is better

public class AddNoticeRequest{
     private String subject;
     private String message;
  class Email extends AddNoticeRequest{
       private String receiver;
 }

Or

public class AddNoticeRequest{
     private String subject;
     private String message;
     private Long timeToLive;
     private String receiver;
}

Then when client request to add Email Notice, some field will be null?

ps : My English is not good, hope ya don't mind :).

Aucun commentaire:

Enregistrer un commentaire