camel-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Matt Raible <m...@raibledesigns.com>
Subject Re: camel cxfrs with swagger.
Date Fri, 17 Oct 2014 14:08:55 GMT
Here's how I did it.

Application.java (to register Camel's servlet):
----
import org.apache.camel.component.servlet.CamelHttpTransportServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.embedded.ErrorPage;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application extends SpringBootServletInitializer {
	private static final String CAMEL_URL_MAPPING = "/rest/*";
	private static final String CAMEL_SERVLET_NAME = "CamelServlet";

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
		return application.sources(Application.class);
	}

	@Bean
	public ServletRegistrationBean servletRegistrationBean() {
		ServletRegistrationBean registration =
				new ServletRegistrationBean(new CamelHttpTransportServlet(), CAMEL_URL_MAPPING);
		registration.setName(CAMEL_SERVLET_NAME);
		return registration;
	}

	@Bean
	public EmbeddedServletContainerCustomizer containerCustomizer() {
		return new EmbeddedServletContainerCustomizer() {
			@Override
			public void customize(ConfigurableEmbeddedServletContainer container) {
				ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/401.html");
				ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/404.html");
				ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500.html");

				container.addErrorPages(error401Page, error404Page, error500Page);
			}
		};
	}
}

SwaggerConfig.java:
----
@Configuration
public class SwaggerConfig implements EnvironmentAware {

    private RelaxedPropertyResolver propertyResolver;

    @Override
    public void setEnvironment(Environment environment) {
    	this.propertyResolver = new RelaxedPropertyResolver(environment, "swagger.");
    }

    /**
     * Swagger Camel Configuration
     */
    @Bean
    public ServletRegistrationBean swaggerServlet() {
    	ServletRegistrationBean swagger = new ServletRegistrationBean(new SpringRestSwaggerApiDeclarationServlet(),
"/swagger/*");
    	Map<String, String> params = new HashMap<>();
    	params.put("base.path", "https://localhost:8443/rest");
    	params.put("api.title", propertyResolver.getProperty("title"));
    	params.put("api.description", propertyResolver.getProperty("description"));
    	params.put("api.termsOfServiceUrl", propertyResolver.getProperty("termsOfServiceUrl"));
    	params.put("api.license", propertyResolver.getProperty("license"));
    	params.put("api.licenseUrl", propertyResolver.getProperty("licenseUrl"));
    	swagger.setInitParameters(params);
    	return swagger;
    }

}

application.properties:
----
# Swagger
swagger.title = My API
swagger.description = A description.
swagger.termsOfServiceUrl = http://yourcompany.com/terms.html
swagger.contact =
swagger.license = Apache 2.0
swagger.licenseUrl = http://www.apache.org/licenses/LICENSE-2.0.html

Hope this helps,

Matt

On Fri, Oct 17, 2014 at 3:18 AM, atg roxx <atgroxx@gmail.com> wrote:
Hi Team,

I am using camel 2.14 and I am trying to expose rest endpoint using CXFRS
and want to use swagger to expose the rest api.


I am able to expose my rest service using cxfrs as described here :
http://camel.apache.org/cxfrs.html

My application is not a web application, and I am using spring and spring
boot for my application.

So I dont have web.xml in my case.

Now for Integrating with Swagger, I looked at
http://camel.apache.org/swagger.html

but it uses web.xml for it.

Is there anyways we can do it without using web.xml.

I looked at the exmple "camel-example-servlet-rest-tomcat" (
http://camel.apache.org/examples.html) explaining swagger integration with
 Camel, but here too web application is used for example i.e web.xml for
this integration.


Could anyone suggest how can we integrate swagger without use  of web.xml


-Cheers,
atg roxx



-- 
720-560-8460
http://raibledesigns.com
http://linkedin.com/in/mraible
Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message