SP.
SP.

Reputation: 363

How to setup Swagger with Spring Boot Camel for REST messaging

I'm following Swagger Java example, but can't make it work with Spring Boot Camel.

I'm running Spring Boot Camel 3.4.0, and have next dependencies in pom.xml:

<!-- Spring Boot -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      <exclusions>
        <exclusion>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-undertow</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

    <!-- Camel -->
    <dependency>
      <groupId>org.apache.camel.springboot</groupId>
      <artifactId>camel-spring-boot-starter</artifactId>
    </dependency>
    <dependency>
      <groupId>org.apache.camel.springboot</groupId>
      <artifactId>camel-stream-starter</artifactId>
    </dependency>
    
    <!-- REST -->
      <dependency>
        <groupId>org.apache.camel.springboot</groupId>
        <artifactId>camel-rest-starter</artifactId>
      </dependency>
       <dependency>
         <groupId>org.apache.camel.springboot</groupId>
         <artifactId>camel-servlet-starter</artifactId>
      </dependency>
      <dependency>
         <groupId>org.apache.camel.springboot</groupId>
         <artifactId>camel-jackson-starter</artifactId>
      </dependency>
      <dependency>
         <groupId>org.apache.camel.springboot</groupId>
         <artifactId>camel-jaxb-starter</artifactId>
      </dependency>
      <dependency>
         <groupId>org.apache.camel.springboot</groupId>
         <artifactId>camel-netty-http-starter</artifactId>
      </dependency>
      <dependency>
         <groupId>org.apache.camel.springboot</groupId>
         <artifactId>camel-http-starter</artifactId>
      </dependency>
      <dependency>
         <groupId>org.apache.camel.springboot</groupId>
         <artifactId>camel-jetty-starter</artifactId>
      </dependency>
      <dependency>
         <groupId>org.apache.camel.springboot</groupId>
         <artifactId>camel-undertow-starter</artifactId>
      </dependency>
      <!--<dependency>
         <groupId>org.apache.camel.springboot</groupId>
         <artifactId>camel-rest-swagger-starter</artifactId>
      </dependency>-->
      <dependency>
         <groupId>org.apache.camel</groupId>
         <artifactId>camel-swagger-java</artifactId>
         <version>3.4.0</version>
      </dependency>

My Router.java is next:

String listenAddress = "192.168.0.100";
        int listenPort = 8080;

        restConfiguration()
                .component("netty-http")
                .scheme("http")
                .host(listenAddress)
                .bindingMode(RestBindingMode.auto)
                .dataFormatProperty("prettyPrint", "true")
                .port(listenPort)
                .contextPath("/")
                // add swagger api-doc out of the box
                .apiContextPath("/api-doc")
                .apiProperty("api.title", "User API").apiProperty("api.version", "1.2.3")
                // and enable CORS
                .apiProperty("cors", "true");

        // this user REST service is json only
        rest("/user").description("User rest service")
            .consumes("application/json").produces("application/json")
            .get("/{id}").description("Find user by id").outType(User.class)
                .param().name("id").type(path).description("The id of the user to get").dataType("int").endParam()
                .log("Swagger REST header id: ${header.id}");

If trying to GET http://192.168.0.100:8080/api-doc I'm getting 404.

This route above should print log in Camel terminal when using REST GET with http://192.168.0.100:8080/user/123 or am I wrong? Can't see what's missing.

Upvotes: 0

Views: 4420

Answers (2)

Dhruv sahu
Dhruv sahu

Reputation: 95

For me adding the configuration :

In routes

String listenAddress = "localhost"; int listenPort = 8003;

    restConfiguration()
            .component("servlet")
            .scheme("http")
            .host(listenAddress)
            .bindingMode(RestBindingMode.auto)
            .dataFormatProperty("prettyPrint", "true")
            .port(listenPort)
            .contextPath("/")
            // add swagger api-doc out of the box
            .apiContextPath("/api-doc")
            .apiProperty("api.title", "User API").apiProperty("api.version", "1.2.3")
            // and enable CORS
            .apiProperty("cors", "true");

In application.properties

server.port=8003

camel.component.servlet.mapping.context-path=/**

URI

http://localhost:8003/api-doc

I am using camel version : 3.9.0


This solution works fine !!!!

Upvotes: 0

Sneharghya Pathak
Sneharghya Pathak

Reputation: 1060

The context path by default is set to /camel/, which means if rest of your configuration is correct, you should be able to see your api-docs at http://192.168.0.100:8080/camel/api-doc

To override it, you need to set the following property in your application.properties file.

camel.component.servlet.mapping.context-path= /*

Upvotes: 0

Related Questions