如何使用Swagger codegen开发一个简单的REST客户端?
我正在学习Swagger以及如何使用Swagger codegen生成REST客户端。我知道如何使用Swagger做文档,我也知道如何用Swagger生成一个简单的REST服务器,但我不知道如何用Swagger codegen生成一个简单的REST客户端。
例如,我有一个简单的应用程序,它是一个REST服务器,我想生成REST客户端。我可以用Swagger codegen做到这一点吗?
REST 服务器的控制器:
package com.dgs.spring.springbootswagger.controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
@RestController
@RequestMapping("/api/v1")
@Api(value = "Employee Management System", description = "Operations pertaining to employee in Employee Management System")
public class EmployeeController {
@Autowired
private EmployeeRepository employeeRepository;
@ApiOperation(value = "View a list of available employees", response = List.class)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Successfully retrieved list"),
@ApiResponse(code = 401, message = "You are not authorized to view the resource"),
@ApiResponse(code = 403, message = "Accessing the resource you were trying to reach is forbidden"),
@ApiResponse(code = 404, message = "The resource you were trying to reach is not found")
})
@GetMapping("/employees")
public List<Employee> getAllEmployees() {
return employeeRepository.findAll();
}
@ApiOperation(value = "Get an employee by Id")
@GetMapping("/employees/{id}")
public ResponseEntity<Employee> getEmployeeById(
@ApiParam(value = "Employee id from which employee object will retrieve", required = true) @PathVariable(value = "id") Long employeeId)
throws ResourceNotFoundException {
Employee employee = employeeRepository.findById(employeeId)
.orElseThrow(() -> new ResourceNotFoundException("Employee not found for this id :: " + employeeId));
return ResponseEntity.ok().body(employee);
}
@ApiOperation(value = "Add an employee")
@PostMapping("/employees")
public Employee createEmployee(
@ApiParam(value = "Employee object store in database table", required = true) @Valid @RequestBody Employee employee) {
return employeeRepository.save(employee);
}
@ApiOperation(value = "Update an employee")
@PutMapping("/employees/{id}")
public ResponseEntity<Employee> updateEmployee(
@ApiParam(value = "Employee Id to update employee object", required = true) @PathVariable(value = "id") Long employeeId,
@ApiParam(value = "Update employee object", required = true) @Valid @RequestBody Employee employeeDetails) throws ResourceNotFoundException {
Employee employee = employeeRepository.findById(employeeId)
.orElseThrow(() -> new ResourceNotFoundException("Employee not found for this id :: " + employeeId));
employee.setEmail(employeeDetails.getEmail());
employee.setLastName(employeeDetails.getLastName());
employee.setFirstName(employeeDetails.getFirstName());
final Employee updatedEmployee = employeeRepository.save(employee);
return ResponseEntity.ok(updatedEmployee);
}
@ApiOperation(value = "Delete an employee")
@DeleteMapping("/employees/{id}")
public Map<String, Boolean> deleteEmployee(
@ApiParam(value = "Employee Id from which employee object will delete from database table", required = true) @PathVariable(value = "id") Long employeeId)
throws ResourceNotFoundException {
Employee employee = employeeRepository.findById(employeeId)
.orElseThrow(() -> new ResourceNotFoundException("Employee not found for this id :: " + employeeId));
employeeRepository.delete(employee);
Map<String, Boolean> response = new HashMap<>();
response.put("deleted", Boolean.TRUE);
return response;
}
}
在那之后,我开发了一个简单的REST客户端:
package com.dgs.restclient.controllers;
@Controller
public class UpdateController {
@Autowired
private EmployeeRestClient restClient;
@GetMapping("/showStartUpdate")
public String showStartCheckin() {
return "startUpdate";
}
@PostMapping("/startUpdate")
public String startCheckIn(@RequestParam("employeeId") Long employeeId, ModelMap modelMap) {
Employee employee = restClient.findEmployee(employeeId);
modelMap.addAttribute("employee", employee);
return "displayEmployeeDetails";
}
@PostMapping("/completeUpdate")
public String completeCheckIn(@RequestParam("employeeId") Long employeeId,
@RequestParam("employeeFirstName") String employeeFirstName,
@RequestParam("employeeLastName") String employeeLastName,
@RequestParam("employeeEmail") String employeeEmail) {
EmployeeUpdateRequest employeeUpdateRequest = new EmployeeUpdateRequest();
employeeUpdateRequest.setId(employeeId);
employeeUpdateRequest.setFirstName(employeeFirstName);
employeeUpdateRequest.setLastName(employeeLastName);
employeeUpdateRequest.setEmail(employeeEmail);
restClient.updateEmployee(employeeUpdateRequest);
return "updateConfirmation";
}
}
员工最忠实客户:
package com.dgs.restclient.integration;
@Component
public class EmployeeRestClientImpl implements EmployeeRestClient {
private static final String EMPLOYEE_REST_URL =
"http://localhost:8080/api/v1/employees/";
@Override
public Employee findEmployee(Long id) {
RestTemplate restTemplate = new RestTemplate();
Employee employee = restTemplate
.getForObject(EMPLOYEE_REST_URL + id, Employee.class);
return employee;
}
@Override
public Employee updateEmployee(EmployeeUpdateRequest request) {
RestTemplate restTemplate = new RestTemplate();
restTemplate
.put(EMPLOYEE_REST_URL + request.getId(), request, Employee.class);
Employee employee = restTemplate
.getForObject(EMPLOYEE_REST_URL + request.getId(), Employee.class);
return employee;
}
}
这个REST客户端是由我开发的,我想知道我是否可以使用Swagger codegen进行此REST客户端开发以及如何进行?我是否只需要在pom.xml中添加swagger-codegen-maven-plugin?我听说过添加这个插件和一个yml文件,Swagger将创建REST客户端。任何反馈将不胜感激!