Swagger and Spring MVC integration - java

when integrate swagger with mvc and use java-based configuration like
#Configuration
#EnableSwagger2
#PropertySource("classpath:application.properties")
public class SwaggerConfig extends WebMvcConfigurerAdapter {
.
.
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
and use url http://localhost:8080/admin-api/admin/swagger-ui.html it give 404.
when i remove addResourceHandlers from SwaggerConfig configuration class and configure through xml like
<mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/" />
<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/" />
with the same url http://localhost:8080/admin-api/admin/swagger-ui.html it run without problems. how can i use java-based configuration?

In swagger - java based configuration
a class exteds from WebMvcConfigurerAdapter, in that class file by override addResourceHandlers like below
#Configuration
public class AppConfig extends WebMvcConfigurerAdapter {
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
super.addResourceHandlers(registry);
}
}

Try flagging your SwaggerConfig with #EnableWebMvc and register "**/**" as another ResourceHandler, as follows:
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("**/**").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
Also have a look at Docket, here is the link for the sample application using Docket API- swagger-example.

Related

Resources are called from /appName but from root

I'm usign spring boot.
When I deploy my app under a AWS balancer, the app is calling some resources from myDir/myAppName. But the style resources are called from root, like myDir/.
E.g. myDir/myAppName/index.html is accesible for loadBalancer. But myDir/myStilesheet.css isn't accesible.
How can I set that resources (css, etc) under myDir/myAppName?
Thanks in advance!
You can implement the WebMvcConfigurer interface and override the void addResourceHandlers(final ResourceHandlerRegistry registry) method.
#Configuration
public class MvcConfig implements WebMvcConfigurer {
#Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
registry.addResourceHandler("myDir/myAppName/myStilesheet.css")
.addResourceLocations("classpath:/resources/static/css/myStilesheet.css");
}}

Internal Server Error When Adding WebMvcConfigurer To Spring Boot Rest Application

I have a Spring Boot app and I wanted to allow other origins to make requests because I got the 'cors' error. So I searched and I found this answer: Annotation CrossOrigin not working in Spring boot witch helped for the endpoints that have no body. On the other hand, those who have a body I get Internal Server Error.
Below is the configuration:
#Configuration
#EnableWebMvc
public class CorsConfiguration implements WebMvcConfigurer {
#Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("http://localhost:63343", "http://localhost:3000", "https://smart-booking-ba548.web.app")
.allowedMethods("GET", "POST", "DELETE", "PUT");
}
}
The controllers have the #RestController annotation and the methods #Get/Post|Mapping. They return a ResponseEntity<Object>.
I solved it by putting the following method in main class.
#Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
#Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("*");
}
};
}

How to serve static pages

I need to serve static index.html page from "/" GET request in spring-web.
My module is included into bigger one, packaged as WAR and deployed to tomcat.
I've tried
#Configuration
#EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
System.out.println("WebMvcConfigurer - addResourceHandlers() function get loaded...");
registry.addResourceHandler("/WEB-INF/classes/index.html")
.addResourceLocations("/WEB-INF/classes/index.html");
}
}
and placed index.html in resources folder. Still 404. Can anybody help me out with understanding what have I done wrong?
The pattern of the ResourceHandler should reflect the query path, in your case /index.html
Resources inside a jar/war /WEB-INF/classes/ can be accessed via classpath:, in your case classpath:/index.html
So your config should have been like this
#Configuration
#EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/index.html")
.addResourceLocations("classpath:/index.html");
}
}
You can simplify if you have more resources with
registry.addResourceHandler("/*.html")
.addResourceLocations("classpath:/");

Can I use two interceptors on same spring boot application?

I have a Spring Boot application that contains two different dashboards, and so each user can access only one. Actually I have one interceptor that secure one dashboard, but the second dashboard needs another interceptor. This is possible?
This is my configuration class:
#Configuration
public class SecurityConfiguration implements WebMvcConfigurer{
#Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new AutorizadorInterceptorEscola());
}
}
I need to add another interceptor to secure the second dashboard.
Create Configuration Class extends with WebMvcConfigurerAdapter
#Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new GuestInterceptor());
registry.addInterceptor(new AdminInterceptor());
}

Spring Boot Maven webapp folder and ResourceHandler

In my Maven project with Spring Boot I have a following folder with a static content(Html, CSS, JavaScript files):
/src/main/webapp/docs/
this is my application properties:
server.port: 8080
server.contextPath: /admin-api
When a user access a following url: http://localhost:8080/admin-api/docs I want to show him a static content from /src/main/webapp/docs/ folder.
I'm trying to configure ResourceHandler in my WebMvcConfigurerAdapter but it doesn't work:
#EnableWebMvc
#Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
String[] staticResourceMappingPath = { "/docs/", "classpath:/docs/" };
registry.addResourceHandler("/docs").addResourceLocations(staticResourceMappingPath);
}
...
}
http://localhost:8080/admin-api/docs returns 404 Not Found.
What am I doing wrong and how to properly configure it ?
Your addResourceHandlers method should be like below.
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/docs/**").addResourceLocations("/docs/");
}
Now if you have a static page say index.html in docs folder then you can access it using http://localhost:8080/admin-api/docs/index.html

Categories

Resources