0

I'm new with Spring and Camel and was told to do this: Create a Sample REST API using Camel and Spring Boot and save it to GITHub, it should include the following: 1 Expose a Restful Service 2 HTTP verb as POST and Media Type can be JSON or XML 3 Define/Create a front end and backend schema’s (JSON or XML Schema’s) – A simple schema should be sufficient. 4 Validate a Message (using a front end JSON or XML Schema) 5 Transform a Message (Front end JSON or XML format to a backend JSON or XML format) 6 Validate a transformed message (using a backend JSON or XML Schema) 7 Build a Mock Service to receive a message and return some sample response

This is what I've done so far but haven't got the Json Validator to work, it just go straight to endpoint. To be honest I don't know if what I'm doing is correct. Do you have any advice please?

@Component
public class BookRoute extends RouteBuilder {

    @Autowired
    private BookService service;

    @Override
    public void configure() throws Exception {

        restConfiguration().component("servlet").bindingMode(RestBindingMode.json);

        rest("/books")
            .consumes(MediaType.APPLICATION_JSON_VALUE)
            .produces(MediaType.APPLICATION_JSON_VALUE)
            .post()
            .type(Book.class).outType(Book.class)
            .to("json-validator:book-fe.json")
            .route().bean(BookService.class, "addBook(${body})").endRest()
            .get().route().bean(BookService.class, "getAllBooks()")
            .end();

    }

}

This is the project structure

enter image description here

Also tried this way

rest("/books")
                .consumes(MediaType.APPLICATION_JSON_VALUE)
                .produces(MediaType.APPLICATION_JSON_VALUE)
                .post().type(Book.class).outType(Book.class).route().to("direct:start").endRest()
                .get().route().bean(BookService.class, "getAllBooks()")
                .end();

        from("direct:start")
                .log("Received Body ${body}")
                .to("json-validator:book-fe.json")
                .bean(BookService.class, "addBook(${body})");

I'm getting the log output

2021-09-16 13:59:28.585  INFO 16688 --- [nio-8080-exec-1] route1: Received Body Book{id=5, name='Book 5', author='Author 5', price=50.0, description='null'}

And this error now

java.lang.NullPointerException: null
    at com.networknt.schema.JsonSchema.combineCurrentUriWithIds(JsonSchema.java:90) ~[json-schema-validator-1.0.38.jar:na]
    at com.networknt.schema.JsonSchema.<init>(JsonSchema.java:72) ~[json-schema-validator-1.0.38.jar:na]
    at com.networknt.schema.JsonSchema.<init>(JsonSchema.java:58) ~[json-schema-validator-1.0.38.jar:na]
    at com.networknt.schema.JsonSchema.<init>(JsonSchema.java:53) ~[json-schema-validator-1.0.38.jar:na]
    at com.networknt.schema.JsonSchemaFactory.newJsonSchema(JsonSchemaFactory.java:266) ~[json-schema-validator-1.0.38.jar:na]
    at com.networknt.schema.JsonSchemaFactory.getSchema(JsonSchemaFactory.java:310) ~[json-schema-validator-1.0.38.jar:na]
    at com.networknt.schema.JsonSchemaFactory.getSchema(JsonSchemaFactory.java:318) ~[json-schema-validator-1.0.38.jar:na]
    at org.apache.camel.component.jsonvalidator.DefaultJsonSchemaLoader.createSchema(DefaultJsonSchemaLoader.java:30) ~[camel-json-validator-3.3.0.jar:3.3.0]
    at org.apache.camel.component.jsonvalidator.JsonValidatorEndpoint.getOrCreateSchema(JsonValidatorEndpoint.java:159) ~[camel-json-validator-3.3.0.jar:3.3.0]
    at org.apache.camel.component.jsonvalidator.JsonValidatorEndpoint.onExchange(JsonValidatorEndpoint.java:92) ~[camel-json-validator-3.3.0.jar:3.3.0]
    at org.apache.camel.support.ProcessorEndpoint$1.process(ProcessorEndpoint.java:63) ~[camel-support-3.3.0.jar:3.3.0]
    at org.apache.camel.support.AsyncProcessorConverterHelper$ProcessorToAsyncProcessorBridge.process(AsyncProcessorConverterHelper.java:67) ~[camel-support-3.3.0.jar:3.3.0]
    at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:168) ~[camel-base-3.3.0.jar:3.3.0]
    at org.apache.camel.processor.errorhandler.RedeliveryErrorHandler$SimpleTask.run(RedeliveryErrorHandler.java:395) ~[camel-base-3.3.0.jar:3.3.0]
    at org.apache.camel.impl.engine.DefaultReactiveExecutor$Worker.schedule(DefaultReactiveExecutor.java:148) [camel-base-3.3.0.jar:3.3.0]
    at org.apache.camel.impl.engine.DefaultReactiveExecutor.scheduleMain(DefaultReactiveExecutor.java:60) [camel-base-3.3.0.jar:3.3.0]
    at org.apache.camel.processor.Pipeline.process(Pipeline.java:147) [camel-base-3.3.0.jar:3.3.0]
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:286) [camel-base-3.3.0.jar:3.3.0]
    at org.apache.camel.impl.engine.DefaultAsyncProcessorAwaitManager.process(DefaultAsyncProcessorAwaitManager.java:83) [camel-base-3.3.0.jar:3.3.0]
    at org.apache.camel.support.AsyncProcessorSupport.process(AsyncProcessorSupport.java:40) [camel-support-3.3.0.jar:3.3.0]
    at org.apache.camel.http.common.CamelServlet.doService(CamelServlet.java:214) [camel-http-common-3.3.0.jar:3.3.0]
    at org.apache.camel.http.common.CamelServlet.service(CamelServlet.java:80) [camel-http-common-3.3.0.jar:3.3.0]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:733) [tomcat-embed-core-9.0.38.jar:4.0.FR]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) [tomcat-embed-core-9.0.38.jar:9.0.38]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.38.jar:9.0.38]
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) [tomcat-embed-websocket-9.0.38.jar:9.0.38]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.38.jar:9.0.38]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.38.jar:9.0.38]
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) [spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) [spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.38.jar:9.0.38]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.38.jar:9.0.38]
    at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) [spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) [spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.38.jar:9.0.38]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.38.jar:9.0.38]
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) [spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) [spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.38.jar:9.0.38]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.38.jar:9.0.38]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) [tomcat-embed-core-9.0.38.jar:9.0.38]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) [tomcat-embed-core-9.0.38.jar:9.0.38]
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) [tomcat-embed-core-9.0.38.jar:9.0.38]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143) [tomcat-embed-core-9.0.38.jar:9.0.38]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.38.jar:9.0.38]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) [tomcat-embed-core-9.0.38.jar:9.0.38]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [tomcat-embed-core-9.0.38.jar:9.0.38]
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374) [tomcat-embed-core-9.0.38.jar:9.0.38]
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) [tomcat-embed-core-9.0.38.jar:9.0.38]
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) [tomcat-embed-core-9.0.38.jar:9.0.38]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590) [tomcat-embed-core-9.0.38.jar:9.0.38]
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.38.jar:9.0.38]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_301]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_301]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.38.jar:9.0.38]
    at java.lang.Thread.run(Thread.java:748) [na:1.8.0_301]
  • Have you checked [this question](https://stackoverflow.com/questions/49010933/apache-camel-rest-dsl-validating-request-payload-and-return-error-response) already? – Roman Vottner Sep 16 '21 at 16:27
  • It looks similar in some aspects, but I need to do it with the Json validator model – Alvaro Mancera Diaz Sep 16 '21 at 16:33
  • I assume `book-fe.json` is your JSON schema and is available in the classpath of the project as indicated in the [documentation](https://camel.apache.org/camel-quarkus/latest/reference/extensions/json-validator.html). A [similar question](https://stackoverflow.com/questions/53246719/how-to-validate-the-json-request-in-camel-rest) is also available that showcases a JSON schema for a concrete JSON instance – Roman Vottner Sep 16 '21 at 16:44
  • @RomanVottner yes I used that similar question as reference first, but they didn't show the whole implementation, that's why I came up with this (which I don't know it's correct), and yes the file is in the classpath – Alvaro Mancera Diaz Sep 16 '21 at 17:03
  • What I further noticed is that your `.to("json-validator:...)` definition is outside of an actual route but in the restdsl endpoint definition. The example in the Camel documentation uses it inside a route (`from(...).to("json-validator:...)`), maybe this explains why it does not. – Roman Vottner Sep 16 '21 at 17:07
  • I have updated the code but I'm still getting errors – Alvaro Mancera Diaz Sep 16 '21 at 18:09
  • if you comment out .to("json-validator:book-fe.json"), will it work? could it be description='null' which leads json-validor failed ? – stewchicken Sep 17 '21 at 08:51
  • Your stacktrace reveals that Camel is calling into `json-validator` so the problem must be in the setup of your `book-fe.json` schema. Without seeing your schema it is hard to pinpoint the actual problem. As one of the comments of an above-mentioned link mentions, there might be a chance that camel only supports version draft version 4 of the schema, as also used in [their sample](https://camel.apache.org/components/latest/json-validator-component.html#_example) – Roman Vottner Sep 17 '21 at 18:50

0 Answers0