0

我正在尝试通过使用 apache camel 构建一些简单的 API 来进行实验。发布请求的请求正文为空,但骆驼不返回错误请求,而是返回 201。

这就是我所做的;

  1. 从 studentreg-api.yaml 生成类 [这是一个有效的 openapi 3 yaml 文档]
  2. 使用camel、spring boot构建REST端点步骤
  3. 配置骆驼休息端点clientRequestValidation(true)
  4. 启动服务器
  5. 打开 /api-docs 并验证是否正确标记了强制属性
  6. 从邮递员向带有空白请求正文的 POST 端点发出请求

预期:400 - 错误请求实际:201

我无法弄清楚我错过了什么。

BaseRouteConfig.java

@ComponentScan
@Configuration
public class BaseRouteConfig extends RouteBuilder {

@Autowired
private Environment environment;

@Value("${studentregistration.api.path}")
private String basePath;


@Bean
ServletRegistrationBean servletRegistrationBean() {
    ServletRegistrationBean servlet = new ServletRegistrationBean(new CamelHttpTransportServlet(), basePath + "/*");
    servlet.setName("CamelServlet");
    return servlet;
}

@Override
public void configure() throws Exception {
    restConfiguration().component("servlet")
            .bindingMode(RestBindingMode.json)
            .dataFormatProperty("pretty-print", "true")
            .enableCORS(true)
            .port(environment.getProperty("server.port", "8085"))
            .contextPath(basePath)
            .apiContextPath("/api-docs")
            .apiVendorExtension(true)
            .apiProperty("api.title", "Student registration")
            .apiProperty("api.version", "0.1")
            .apiProperty("cors", "true")
            .clientRequestValidation(true);
    }
}

RegisterStudentRoute.java

@Component
public class RegisterStudentRoute extends RouteBuilder {

@Override
    public void configure() throws Exception {
        rest("/api/").consumes(MediaType.APPLICATION_JSON_VALUE)
                .produces(MediaType.APPLICATION_JSON_VALUE)
                .post("/registration")
                .type(RegisterStudentRequest.class)
                .clientRequestValidation(true)
                .outType(RegisterStudentResponse.class)
                .responseMessage().code(HttpStatus.CREATED.value())
                .endResponseMessage()
                .route().routeId("create-student-registration")
                .setHeader(Exchange.HTTP_RESPONSE_CODE, simple("201"))
                .to("log:success")
                .end().endRest();
    }
}

pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>camel-rest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>camel-rest</name>
    <description>Demo project for Spring Boot &amp; camel rest</description>
    <properties>
        <java.version>16</java.version>
        <camel.version>3.10.0</camel.version>
        <openapi-generator.version>5.1.0</openapi-generator.version>
        <springfox.version>2.9.2</springfox.version>
        <jackson-databind.version>0.2.1</jackson-databind.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.camel.springboot</groupId>
            <artifactId>camel-servlet-starter</artifactId>
            <version>3.9.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.camel.springboot</groupId>
            <artifactId>camel-jackson-starter</artifactId>
            <version>3.9.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.camel.springboot</groupId>
            <artifactId>camel-openapi-java-starter</artifactId>
            <version>3.9.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.camel.springboot</groupId>
            <artifactId>camel-spring-boot-starter</artifactId>
            <version>3.9.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.camel.springboot</groupId>
            <artifactId>camel-http-starter</artifactId>
            <version>3.9.0</version>
        </dependency>
        <dependency>
            <groupId>org.openapitools</groupId>
            <artifactId>openapi-generator</artifactId>
            <version>${openapi-generator.version}</version>
        </dependency>
        <dependency>
            <groupId>org.openapitools</groupId>
            <artifactId>jackson-databind-nullable</artifactId>
            <version>${jackson-databind.version}</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>${springfox.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.4.5</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <!-- RELEASE_VERSION -->
                <version>5.1.0</version>
                <!-- /RELEASE_VERSION -->
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>${project.basedir}/src/main/resources/api.yaml</inputSpec>
                            <generatorName>spring</generatorName>
                            <apiPackage>com.sample.camel-rest.api</apiPackage>
                            <modelPackage>com.sample.camel-rest.model</modelPackage>

                            <supportingFilesToGenerate>
                                ApiUtil.java
                            </supportingFilesToGenerate>
                            <configOptions>
                                <delegatePattern>true</delegatePattern>
                                <outputDir>${project.basedir}/generated</outputDir>
                            </configOptions>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>
4

1 回答 1

0

行!我想出了一种更简单的方法来处理验证。向 bean-validator 添加路由解决了这个问题!如果我们需要构建自定义错误消息,或者如果您需要基于故障构建错误模型,您可以使用骆驼处理器和 javax.validator。

RegisterStudentRoute.java

@Component
public class RegisterStudentRoute extends RouteBuilder {

    @Autowired
    RegisterStudentRequestValidator registerStudentRequestValidator;

    @Override
    public void configure() throws Exception {
        rest("/api/").consumes(MediaType.APPLICATION_JSON_VALUE)
                .produces(MediaType.APPLICATION_JSON_VALUE)
                .post("/registration")
                .type(RegisterStudentRequest.class)
                .clientRequestValidation(true)
                .param().name("request-id").dataType("string")
                .type(RestParamType.header).required(true)
                .endParam()
                .outType(RegisterStudentResponse.class)
                .responseMessage().code(HttpStatus.CREATED.value())
                .endResponseMessage()
                .route().routeId("create-student-registration")
//                .process(registerStudentRequestValidator)
                .to("direct:validateReq")
                .setHeader(Exchange.HTTP_RESPONSE_CODE, simple("201"))
                .to("log:success")
                .end().endRest();

        from("direct:validateReq")
                .to("bean-validator://x")
                .to("mock:end");
    }
于 2021-05-23T20:33:54.607 回答