0

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?

2 Answers 2

1

First of all it seems like you should use CrudRepository#findAll(java.lang.Iterable<ID>) for searching your entities by multiple ids.

Also in your specific case it's redundant to create a separate CustomerSearchDto as a holder for id - it's better to just operate with longs.

So just pass List<Long> ids as a parameter in your controller (don't forget annotate this parameter as @RequestBody or @RequestParam depending where do you want to specify these ids - in url or in body), and call CrudRepository#findAll(ids) from your service class.

Sign up to request clarification or add additional context in comments.

9 Comments

after passing id to service, what should i return from controller class?
customerDto customerDto = new customerDto; List<customerDto> customerDtoList = customerService.findcustomerIds(ids); return ResponseEntity.ok(customerDto);
here i haven't use customerDtoList. but if i use it return then error happens.
if you find customers successfully - you should return customerDtoList, if you have nothing to return - you should either throw an exception "There are no any customers with such ids" or just return empty list, depending on your requirements
thanks @star67. can you make an example of customerdtolist class ? what methods should i put into it ?
|
0

You need not define separate input and output classes, instead, you can build and return a Map or a List as per your use-case. Also for the input you can accept List which will contain List of ids of customers you would like to retrieve.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.