Spring Boot 使用OpenAPI生成的接口时,服务不会被注入

k7fdbhmy  于 2023-11-17  发布在  Spring
关注(0)|答案(1)|浏览(109)

当使用openApi生成的接口时,服务不会在MockMvc测试中注入。

控制器

@RestController
@Slf4j
@RequiredArgsConstructor
public class MyController implements MyApi {
    private final CodeService codeService;

    @Override
    @PutMapping("/path")
    public ResponseEntity<Void> putStuff(@RequestBody @Valid final Stuff stuff) {
        // do stuff
        return ResponseEntity.ok().build();
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    private ResponseEntity<ResponseWithError> handleException(final MethodArgumentNotValidException e, HttpServletRequest request) {
        log.error(e.getMessage());

        final ResponseWithError body = new ResponseWithError();
        body.setTimestamp(TimeUtil.getCurrentTimestampString());
        body.setId(UUID.randomUUID().toString());
        body.setMessage(e.getMessage());
        body.setPath(request.getRequestURI());
        body.code(codeService.getErrors(e.getAllErrors()));

        return ResponseEntity.badRequest().body(body);
    }
}

字符串

MyApi

@Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2023-11-15T16:01:27.640187200+01:00[Europe/Berlin]")
@Validated
@Tag(name = "stuff", description = "handles stuff")
public interface MyApi {

    default Optional<NativeWebRequest> getRequest() {
        return Optional.empty();
    }

    /**
     * PUT /stuff : creates stuff
     * creates stuff
     *
     * @param stuff (required)
     * @return OK (status code 200)
     * or CREATED (status code 201)
     * or UNAUTHORIZED (status code 401)
     * or BAD REQUEST (status code 400)
     * or INTERNAL SERVER ERROR (status code 500)
     */
    @Operation(
            operationId = "putStuff",
            summary = "creates stuff",
            description = "creates stuff",
            tags = {"stuff"},
            responses = {
                    @ApiResponse(responseCode = "200", description = "OK"),
                    @ApiResponse(responseCode = "201", description = "CREATED"),
                    @ApiResponse(responseCode = "401", description = "UNAUTHORIZED", content = {
                            @Content(mediaType = "application/json", schema = @Schema(implementation = ResponseWithError.class))
                    }),
                    @ApiResponse(responseCode = "400", description = "BAD REQUEST", content = {
                            @Content(mediaType = "application/json", schema = @Schema(implementation = ResponseWithError.class))
                    }),
                    @ApiResponse(responseCode = "500", description = "INTERNAL SERVER ERROR", content = {
                            @Content(mediaType = "application/json", schema = @Schema(implementation = ResponseWithError.class))
                    })
            },
            security = {
                    @SecurityRequirement(name = "bearerAuth")
            }
    )
    @RequestMapping(
            method = RequestMethod.PUT,
            value = "/path",
            produces = {"application/json"},
            consumes = {"application/json"}
    )
    default ResponseEntity<Void> putStuff(
            @Parameter(name = "stuff", description = "", required = true) @Valid @RequestBody Stuff stuff
    ) {
        return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
    }
}

测试

@WebMvcTest(controllers = {MyController.class}, excludeAutoConfiguration = {SecurityAutoConfiguration.class})
class MyControllerTest {
    @Autowired
    private MockMvc mvc;

    @MockBean
    private CodeService codeService;

    @Test
    void invalidRequest() throws Exception {
        // @GIVEN
        final Stuff stuff = new Stuff();
        stuff.setThings("");

        final String body = new ObjectMapper().writeValueAsString(stuff);

        given(codeService.getErrors(anyList())).willReturn(MY_VALIDATION_ERROR);

        // @WHEN
        mvc.perform(MockMvcRequestBuilders.put("/path")
                                          .contentType("application/json")
                                          .content(body));

        // @THEN
        // Assertions
    }
}

  • 我使用的是Spring-Boot 2.7.12
  • CodeService使用@Component进行注解
  • MyApi是OpenAPI生成的接口

当执行测试时,它触发异常并运行到处理程序中。一切正常,直到它遇到body.code(codeService.getErrors(e.getAllErrors()));行。不知何故,codeService没有被注入,但是-这是我不理解的事情-当我不使用生成的接口MyApi时,codeService**被注入,测试正常运行。
有人能解释一下,为什么当我使用MyApi接口时,服务没有被注入?有没有一种方法可以让测试与生成的API一起工作?

编辑

  • 似乎是@Validated注解导致了这种行为。不管我是直接注解控制器还是使用生成的接口。有人能解释一下为什么吗?
7hiiyaii

7hiiyaii1#

正如M.代努姆评论的那样,我把访问器改为公共的。不,它工作!

相关问题