SpringBoot code that assumes your schema is available at http://localhost:8080/v3/api-docs and the payload you want to validate corresponds to the path /endpoint/myentity.
This will allow pasting your JSON payload in the input form in the swagger-ui, and then click Try it out to do the validation. springdoc-openapi-starter-webmvc-ui is used for the Swagger UI and underlying dependencies.
import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; import com.atlassian.oai.validator.OpenApiInteractionValidator; import com.atlassian.oai.validator.model.Request; import com.atlassian.oai.validator.model.Response; import com.atlassian.oai.validator.model.SimpleResponse; import com.atlassian.oai.validator.report.ValidationReport; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.responses.ApiResponses; @RestController public class SwaggerValidator { @Operation( summary = "Validate JSON payload against Schema", description = "Validate provided JSON response string. " + "Paste your JSON in the request body to check if it adheres to the schema rules." ) @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Validation successful"), @ApiResponse(responseCode = "400", description = "Validation failed due to invalid JSON schema") }) @PostMapping("/validate") public ResponseEntity<?> validateSchema(@RequestBody String jsonBody) { OpenApiInteractionValidator validator = OpenApiInteractionValidator.createForSpecificationUrl("http://localhost:8080/v3/api-docs").build(); Response response = SimpleResponse.Builder .status(200) .withBody(jsonBody) .withContentType("application/json") .build(); ValidationReport validationResult; try { validationResult = validator.validateResponse("/endpoint/myentity", Request.Method.GET, response); if (!validationResult.hasErrors()) { return new ResponseEntity<>(validationResult, HttpStatus.OK); } else { return new ResponseEntity<>(validationResult.getMessages(), HttpStatus.BAD_REQUEST); } } catch (Exception e) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.toString()); } } }
a minimalistic maven 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>3.3.0</version> <relativePath/> </parent> <groupId>com.example.openapi</groupId> <artifactId>example-openapi-spec</artifactId> <version>0.0.1-SNAPSHOT</version> <name>spring-boot-complete</name> <description>API Definition</description> <properties> <java.version>17</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springdoc</groupId> <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId> <version>2.6.0</version> </dependency> <dependency> <groupId>com.atlassian.oai</groupId> <artifactId>swagger-request-validator-core</artifactId> <version>2.43.0</version> </dependency> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
Once you add the dependencies and the classes to your SpringBoot project, run mvn spring-boot:run, open the swagger UI at http://localhost:8080/swagger-ui.html, and validate your JSON payload by pasting the raw string into the POST form body in swagger.
deprecated answer for Swagger v2
Minimal java code to do offline validation of a .json payload file against a .yaml swagger spec using https://github.com/bjansen/swagger-schema-validator:
Reference the dependencies in your own project as described in the swagger-schema-validator readme or clone locally with git clone https://github.com/bjansen/swagger-schema-validator.git.
Copy your .yaml and .json files into the src/test/resources folder under your test root folder.
Create a test class containing something along the lines (make sure to change "/definitions/MyPayloadObjectMustBeSetHere" to point to your own definition):
import com.github.bjansen.ssv.SwaggerValidator; import com.github.fge.jsonschema.core.report.ProcessingReport; import static org.junit.jupiter.api.Assertions.assertEquals; @Test void SwaggerSpecTest() { InputStream spec = getClass().getResourceAsStream("/api/swagger.yaml"); SwaggerValidator validator = SwaggerValidator.forYamlSchema(new InputStreamReader(spec)); InputStreamReader sample = new InputStreamReader(getClass().getResourceAsStream("/api/payload.json")); ProcessingReport report = validator.validate(CharStreams.toString(sample), "/definitions/MyPayloadObjectMustBeSetHere"); assertEquals("success", report.toString()); // force printing the errors/warnings }