У меня есть общий Rest Controller, общий для всех контроллеров
Код: Выделить всё
public class GenericController{
@Autowired
private GenericService genericService;
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity findById(@PathVariable P id) {
return new ResponseEntity(genericService.findById(id), HttpStatus.OK);
}
@RequestMapping(value = "/findAll", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity findAll() {
return new ResponseEntity(genericService.findAll(), HttpStatus.OK);
}
@RequestMapping(value = "/create", method = RequestMethod.POST)
@ResponseBody
public ResponseEntity create(@RequestBody @Valid E e) {
return new ResponseEntity(genericService.save(e), HttpStatus.OK);
}
}
Код: Выделить всё
@RestController
@RequestMapping("/Customer")
public class CustomerController extends GenericController implements GraphQLQueryResolver{
public List findAllCustomer(){//This method is only for GraphQL
return findAll();
}
}
Код: Выделить всё
@RestController
@RequestMapping("/Product")
public class ProductController extends GenericController implements GraphQLQueryResolver{
public List findAllProduct(){//This method is only for GraphQL
return findAll();
}
}
Код: Выделить всё
type CustomerEntity {
id: ID!
name: String!
email: String!
}
type ProductEndity {
id: ID!
name: String!
price: Int!
}
type Query {
findAllCustomer: [CustomerEntity]
findAllProduct: [ProductEndity]
}
p>
С помощью RestAPI я использую одно имя метода (findAll) для двух путей /Customer и /Product
Код: Выделить всё
http://localhost:7888/Customer/findAll
http://localhost:7888/Product/findAll
Код: Выделить всё
query {
findAllCustomer{
id name email
}
}
Код: Выделить всё
query {
findAllProduct{
id name price
}
}
Как я могу использовать одно имя метода (findAll) как для клиента, так и для продукта? Нет необходимости писать отдельные методы для каждого контроллера. , аналогичный RestAPI
Просто так (я так думаю

Код: Выделить всё
query {
Customer{
findAll{
id name email
}
}
}
Код: Выделить всё
query {
Product{
findAll{
id name price
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/739 ... java-class