I have a table Customers and 2 column customer_id and customer_name. I want to send id list request with json and return corresponding customer names. But I could not handle dto objects and controller-service architecture.
input dto:
public class CustomerSearchDto extends BaseDto {
@ApiModelProperty(
example = "1",
value = "Customer Id",
required = true,
dataType = "Long"
)
private Long id;
}
outputdto:
public class CustomerDto extends BaseDto {
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Controller Class:
@ApiOperation(
value = "Return Customer",
response = Customer.class
)
@PostMapping(value = Endpoint.RRESOURCE_CUSTOMER_GROUP_BY_ID)
public ResponseEntity<CustomerDto> getCustomersById(@RequestBody @Validated CustomerSearchDto CustomerSearchDto) {
CustomerDto CustomerDto = new CustomerDto;
List<CustomerDto> CustomerDtoList = CustomerService.findCustomerByIds(ids);
return ResponseEntity.ok(CustomerDto);
}
Service class method:
@Transactional
public List<CustomerDto> findCustomerByIds(List<Long> customerIds) {
List<Customer> customerList = repository.findCustomerById(CustomerIds);
return mapper.mapAsList(CustomerList, CustomerDto.class);
}
There are some mistakes in controller class. And also I am not sure about that should i define both for input and output different dto classes?