我正在尝试通过使用 apache camel 构建一些简单的 API 来进行实验。发布请求的请求正文为空,但骆驼不返回错误请求,而是返回 201。
这就是我所做的;
- 从 studentreg-api.yaml 生成类 [这是一个有效的 openapi 3 yaml 文档]
- 使用camel、spring boot构建REST端点步骤
- 配置骆驼休息端点
clientRequestValidation(true)
- 启动服务器
- 打开 /api-docs 并验证是否正确标记了强制属性
- 从邮递员向带有空白请求正文的 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 & 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>