gpt4 book ai didi

java - 在 Ratpack Handler 中转换 Springboot Controller 类

转载 作者:太空宇宙 更新时间:2023-11-04 10:52:44 24 4
gpt4 key购买 nike

我无法使用 SpringBoot 应用程序将现有的 REST Controller 转换为 Ratpack 处理程序。

请任何人帮助我完成这件事。下面是我的 Controller 类:

@RestController
@RequestMapping("/api")
public class RestApiController {

public static final Logger logger = LoggerFactory.getLogger(RestApiController.class);

@Autowired
private EmployeeService EmployeeService;

/**
* Service will retrieve data for particular Employee
*
* @param id Employee Id
* @return ResponseEntity of Employee
*/
@GetMapping(value = "/Employee/{id}")
public Employee getEmployee(@PathVariable("id") String id) {
logger.info("Fetching Employee with id {}", id);
return EmployeeService.findById(id);
}

/**
* Service will create Employee data into the System
*
* @param Employee Data of the Employee
* @param UriComponentsBuilder
* @return ResponseEntity of String
*/
@PostMapping(value = "/Employee/")
public void createEmployee(@RequestBody Employee Employee) {
logger.info("Creating Employee : {}", Employee);
EmployeeService.saveEmployee(Employee);
}
}

最佳答案

public class RatpackMain {

public static void main(String... args) throws Exception {
RatpackServer.start(server -> {
server.serverConfig(config -> {
// load config, set port, etc.
}).registry(Guice.registry(bindings -> {
bindings
.bind(EmployeeService.class, EmployeeServiceImpl.class).in(Scopes.SINGLETON)
.bind(EmplyeeChain.class).in(Scopes.SINGLETON)
.bind(CreateEmployeeHandler.class).in(Scopes.SINGLETON)
.bind(GetEmployeeHandler.class).in(Scopes.SINGLETON);
// other bindings you may have
})).handlers(chain -> {
chain.prefix("api/employee", EmployeeChain.class);
});
});
}
}

public class EmployeeChain implements Action<Chain> {
@Override
public void execute(Chain chain) {
chain.post(CreateEmployeeHandler.class)
.get(":id", GetEmployeeHandler.class);
}
}

public class CreateEmployeeHandler implements Handler {

public static final Logger LOG = LoggerFactory.getLogger(CreateEmployeeHandler.class);

private EmployeeService employeeService;

@Inject
public CreateEmployeeHandler(EmployeeService employeeService) {
this.employeeService = employeeService;
}

@Override
public void handle(Context ctx) throws Exception {
ctx.parse(Employee.class).then(employee -> {
LOG.info("Creating employee: {}", employee);
employeeService.saveEmployee(employee);
ctx.getResponse().status(201).send();
});
}
}

public class GetEmployeeHandler implements Handler {

public static final Logger LOG = LoggerFactory.getLogger(GetEmployeeHandler.class);

private EmployeeService employeeService;

@Inject
public GetEmployeeHandler(EmployeeService employeeService) {
this.employeeService = employeeService;
}

@Override
public void handle(Context ctx) throws Exception {
String id = ctx.getAllPathTokens().get("id");
LOG.info("Fetching employee with id {}", id);
employeeService.findById(id)
.onError(ctx::error)
.then(ctx::render);
}
}

public interface EmployeeService {

void saveEmployee(Employee employee);

Promise<Employee> findById(String id);
}

假设 EmployeeService 调用数据库,那么任何阻塞操作都应该包含在对 Blocking.get() 的调用中。

Ratpack Documentation - Performing Blocking Operations

关于java - 在 Ratpack Handler 中转换 Springboot Controller 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47633556/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com