Skip to content

Instantly share code, notes, and snippets.

@wenqiglantz
Created November 24, 2022 07:00
Show Gist options
  • Save wenqiglantz/30b654361ec69b566662f640322eddda to your computer and use it in GitHub Desktop.
Save wenqiglantz/30b654361ec69b566662f640322eddda to your computer and use it in GitHub Desktop.
@RestController
@RequestMapping(value = "/customers", produces = MediaType.APPLICATION_JSON_VALUE)
@ImportRuntimeHints(CustomerController.CustomerControllerRuntimeHints.class)
public class CustomerController {
private final CustomerService customerService;
public CustomerController(CustomerService customerService) {
this.customerService = customerService;
}
@PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity createCustomer(@RequestBody CustomerVO customerVO, UriComponentsBuilder uriBuilder)
throws Exception {
CustomerVO newCustomerVO = customerService.saveCustomer(customerVO);
URI location = uriBuilder
.path("/customers/{customerId}")
.buildAndExpand(newCustomerVO.getCustomerId())
.toUri();
return ResponseEntity.created(location)
.contentType(MediaType.valueOf(MediaType.APPLICATION_JSON_VALUE))
.body(CustomerVO.builder()
.customerId(newCustomerVO.getCustomerId())
.firstName(newCustomerVO.getFirstName())
.lastName(newCustomerVO.getLastName())
.build());
}
static class CustomerControllerRuntimeHints implements RuntimeHintsRegistrar {
@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
hints.reflection()
.registerConstructor(
CustomerService.class.getConstructors()[0], ExecutableMode.INVOKE)
.registerMethod(ReflectionUtils.findMethod(
CustomerService.class, "saveCustomer", CustomerVO.class), ExecutableMode.INVOKE);
hints.resources().registerPattern("db/changelog/db.changelog-master.xml");
hints.resources().registerPattern("db/changelog/db.changelog-1.0.xml");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment