我无法使用SpringBoot应用程序将现有的REST控制器转换为Ratpack Handler。

请任何人帮助我完成此任务。下面是我的控制器类:

@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

10-06 14:26