Spring boot do not change HTTP code in case of exception - spring

I have trouble controlling the HTTP response code of my Spring Boot Rest server. The controller advice change the header www-authenticate but I keep getting 404 not found (tested using Postman).
Here is my code (made to generate 401 all the time).
The Configuration class:
#Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter
{
public SecurityConfig()
{
super(false);
}
#Override
protected AuthenticationManager authenticationManager() throws Exception
{
return new ProviderManager(Arrays.asList((AuthenticationProvider) new CustomAuthenticationProvider()));
}
#Override
protected void configure(HttpSecurity http) throws Exception
{
http.httpBasic().authenticationEntryPoint(new MyAuthenticationEntryPoint());
http.httpBasic()
.and().authorizeRequests().anyRequest().hasAuthority("USER")
.and().csrf().disable();
}
}
The controller advice to indicate what to do in case of exception (actually trigger according to my console and the WWW-Authenticate value):
#ControllerAdvice
public class MyAuthenticationEntryPoint implements AuthenticationEntryPoint
{
#Override
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException)
throws IOException, ServletException
{
System.out.println("there");
response.setHeader("WWW-Authenticate", "Unauthorized test");
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, authException.getMessage());
}
}
The authentication provider that will always throw a BadCredentialsException.
public class CustomAuthenticationProvider implements AuthenticationProvider
{
public CustomAuthenticationProvider()
{
}
#Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
throw new BadCredentialsException("Bad credentials exception");
}
#Override
public boolean supports(Class<?> pClass)
{
return (pClass == UsernamePasswordAuthenticationToken.class);
}
}
Any idea what I did wrong?
Stacktrace:
..57,398 [DEBUG](o.s.web.servlet.DispatcherServlet) - DispatcherServlet with name 'dispatcherServlet' processing POST request for [/error]
..57,399 [TRACE](o.s.web.servlet.DispatcherServlet) - Testing handler map [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping#1d9af731] in DispatcherServlet with name 'dispatcherServlet'
..57,401 [TRACE](o.s.web.servlet.handler.SimpleUrlHandlerMapping) - No handler mapping found for [/error]
..57,401 [TRACE](o.s.web.servlet.DispatcherServlet) - Testing handler map [springfox.documentation.spring.web.PropertySourcedRequestMappingHandlerMapping#445058e8] in DispatcherServlet with name 'dispatcherServlet'
..57,404 [TRACE](o.s.web.servlet.DispatcherServlet) - Testing handler map [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#5423a17] in DispatcherServlet with name 'dispatcherServlet'
..57,405 [DEBUG](o.s.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) - Looking up handler method for path /error
..57,412 [DEBUG](o.s.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) - Did not find handler method for [/error]
..57,412 [TRACE](o.s.web.servlet.DispatcherServlet) - Testing handler map [org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WelcomePageHandlerMapping#347b370c] in DispatcherServlet with name 'dispatcherServlet'
..57,413 [TRACE](o.s.web.servlet.DispatcherServlet) - Testing handler map [org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping#356fa0d1] in DispatcherServlet with name 'dispatcherServlet'
..57,413 [TRACE](o.s.web.servlet.handler.BeanNameUrlHandlerMapping) - No handler mapping found for [/error]
..57,413 [TRACE](o.s.web.servlet.DispatcherServlet) - Testing handler map [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping#31533eb1] in DispatcherServlet with name 'dispatcherServlet'
..57,413 [DEBUG](o.s.web.servlet.handler.SimpleUrlHandlerMapping) - Matching patterns for request [/error] are [/**]
..57,414 [DEBUG](o.s.web.servlet.handler.SimpleUrlHandlerMapping) - URI Template variables for request [/error] are {}
..57,416 [DEBUG](o.s.web.servlet.handler.SimpleUrlHandlerMapping) - Mapping [/error] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[ServletContext resource [/], class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver#3c6fb501]]] and 1 interceptor
..57,418 [TRACE](o.s.web.servlet.DispatcherServlet) - Testing handler adapter [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#7ecb9e17]
..57,418 [TRACE](o.s.web.servlet.DispatcherServlet) - Testing handler adapter [org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter#4dac40b]
..57,427 [TRACE](o.s.web.servlet.resource.ResourceHttpRequestHandler) - Applying "invalid path" checks to path: error
..57,429 [TRACE](o.s.web.servlet.resource.PathResourceResolver) - Resolving resource for request path "error"
..57,429 [TRACE](o.s.web.servlet.resource.PathResourceResolver) - Checking location: ServletContext resource [/]
..57,429 [TRACE](o.s.web.servlet.resource.PathResourceResolver) - No match for location: ServletContext resource [/]
..57,429 [TRACE](o.s.web.servlet.resource.PathResourceResolver) - Checking location: class path resource [META-INF/resources/]
..57,430 [TRACE](o.s.web.servlet.resource.PathResourceResolver) - No match for location: class path resource [META-INF/resources/]
..57,430 [TRACE](o.s.web.servlet.resource.PathResourceResolver) - Checking location: class path resource [resources/]
..57,431 [TRACE](o.s.web.servlet.resource.PathResourceResolver) - No match for location: class path resource [resources/]
..57,431 [TRACE](o.s.web.servlet.resource.PathResourceResolver) - Checking location: class path resource [static/]
..57,432 [TRACE](o.s.web.servlet.resource.PathResourceResolver) - No match for location: class path resource [static/]
..57,432 [TRACE](o.s.web.servlet.resource.PathResourceResolver) - Checking location: class path resource [public/]
..57,432 [TRACE](o.s.web.servlet.resource.PathResourceResolver) - No match for location: class path resource [public/]
..57,432 [TRACE](o.s.web.servlet.resource.ResourceHttpRequestHandler) - No matching resource found - returning 404
..57,432 [DEBUG](o.s.web.servlet.DispatcherServlet) - Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
..57,432 [TRACE](o.s.web.servlet.DispatcherServlet) - Cleared thread-bound request context: org.apache.catalina.core.ApplicationHttpRequest#268be1ed
..57,432 [DEBUG](o.s.web.servlet.DispatcherServlet) - Successfully completed request
..57,433 [TRACE](o.s.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext) - Publishing event in org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext#40ef3420: ServletRequestHandledEvent: url=[/error]; client=[0:0:0:0:0:0:0:1]; method=[POST]; servlet=[dispatcherServlet]; session=[null]; user=[null]; time=[37ms]; status=[OK]
..57,433 [DEBUG](o.s.beans.factory.support.DefaultListableBeanFactory) - Returning cached instance of singleton bean 'delegatingApplicationListener'

Related

Error while configuring View Resolver for mustache template engine

I have the following configuration file in spring-boot project :
#Configuration
public class AppConfig {
#Bean
public ViewResolver mustacheViewResolver() {
MustacheViewResolver viewResolver = new MustacheViewResolver();
viewResolver.setPrefix("/templates/");
viewResolver.setSuffix(".mustache");
viewResolver.setOrder(1);
return viewResolver;
}
}
When I run my application, I am getting the following error:
Description:
The bean 'mustacheViewResolver', defined in class path resource [org/springframework/boot/autoconfigure/mustache/MustacheServletWebConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [com/example/demo/AppConfig.class] and overriding is disabled.
Action:
Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true
I am not sure if I am configuring the view Resolver properly
Error after removing the configuration class:
o.s.w.s.v.ContentNegotiatingViewResolver : Selected '*/*' given [*/*]
o.s.w.servlet.view.InternalResourceView : View name 'tweets.mustache', model {tweets=null}
o.s.w.servlet.view.InternalResourceView : Forwarding to [tweets.mustache]
o.s.web.servlet.DispatcherServlet : "FORWARD" dispatch for GET "/tweets.mustache?email=tim#gmail.com", parameters={masked}
o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped to ResourceHttpRequestHandler [classpath [META-INF/resources/], classpath [resources/], classpath [static/], classpath [public/], ServletContext [/]]
o.s.w.s.r.ResourceHttpRequestHandler : Resource not found
o.s.web.servlet.DispatcherServlet : Exiting from "FORWARD" dispatch, status 404
o.s.web.servlet.DispatcherServlet : Completed 404 NOT_FOUND
o.s.web.servlet.DispatcherServlet : "ERROR" dispatch for GET "/error?email=tim#gmail.com", parameters={masked}
s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#error(HttpServletRequest)
o.s.w.s.m.m.a.HttpEntityMethodProcessor : Using 'application/json', given [*/*] and supported [application/json, application/*+json, application/json, application/*+json]
o.s.w.s.m.m.a.HttpEntityMethodProcessor : Writing [{timestamp=, status=404, error=Not Found, path=/tweet2}]
o.s.web.servlet.DispatcherServlet : Exiting from "ERROR" dispatch, status 404
#GetMapping("/tweet2")
public ModelAndView getTweetsByEmail(#RequestParam String email) {
HQLExample.insertRecords();
ModelAndView modelAndView = new ModelAndView("tweets.mustache");
List<Tweet> tweets = tweetMap.get(email);
modelAndView.getModel().put("tweets",tweets);
return modelAndView;
}
Assuming you have added spring-boot-starter-mustache as a dependency (to easily include all needed dependencies). When Spring Boot detects Mustache on the classpath it will automatically configure the MustacheViewResolver which will load Mustache templates from /templates on the classpath. The files should end with .mustache.
With this in mind, just remove your AppConfig class as it interferes with the auto configuration.
In your controller the name of the view is the name you have but without the .mustache that will be added by the ViewResolver.
So in short you should remove things and it will work. Do more with less in this case.

Spring cloud GCP com.google.cloud.storage.StorageException access_token not found error

Spring boot-2.3.10, Spring Cloud Gcp: 1.2.8
I'm trying to access specific image pattern **(/resources/images/specific_folder/****) from GC Storage. For that I wrote the resource handler as shown below:
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
log.info("Setting the resource location {}", gcStorageLocation);
registry.addResourceHandler("/resources/images/specific_folder/**").addResourceLocations("gs:bucket_name/storage/images/specific_folder/").setCachePeriod(3600).resourceChain(true)
.addResolver(new GcStorageResolver());
}
GcStorageResolver.java extends AbstractResourceResolver.java
#Override
protected Resource resolveResourceInternal(#Nullable HttpServletRequest request, String requestPath, List<? extends Resource> locations,
ResourceResolverChain chain) {
log.info("resolveResourceInternal called for request: {}, requestPath: {}", request.getRequestURL(), requestPath);
return getResource(requestPath, request, locations);
}
I verified that a valid GoogleStorageResource is being returned along with credential. But somewhere in the spring chain, I'm getting the below error:
2021-06-25 15:40:23.366 ERROR 4676 --- [nio-8080-exec-1]
o.a.c.c.C.[.[.[.[dispatcherServlet] 175 : Servlet.service() for
servlet [dispatcherServlet] in context with path [] threw exception
[Request processing failed; nested exception is
com.google.cloud.storage.StorageException: Error parsing token refresh
response. Expected value access_token not found.] with root cause
java.io.IOException: Error parsing token refresh response. Expected
value access_token not found. at
com.google.auth.oauth2.OAuth2Utils.validateString(OAuth2Utils.java:113)
~[google-auth-library-oauth2-http-0.22.1.jar:?] at
com.google.auth.oauth2.ServiceAccountCredentials.refreshAccessToken(ServiceAccountCredentials.java:449)
~[google-auth-library-oauth2-http-0.22.1.jar:?] at
com.google.auth.oauth2.OAuth2Credentials.refresh(OAuth2Credentials.java:157)
~[google-auth-library-oauth2-http-0.22.1.jar:?] at
com.google.auth.oauth2.OAuth2Credentials.getRequestMetadata(OAuth2Credentials.java:145)
~[google-auth-library-oauth2-http-0.22.1.jar:?] at
com.google.auth.oauth2.ServiceAccountCredentials.getRequestMetadata(ServiceAccountCredentials.java:603)
~[google-auth-library-oauth2-http-0.22.1.jar:?] at
com.google.auth.http.HttpCredentialsAdapter.initialize(HttpCredentialsAdapter.java:91)
~[google-auth-library-oauth2-http-0.22.1.jar:?] at
com.google.cloud.http.HttpTransportOptions$1.initialize(HttpTransportOptions.java:159)
~[google-cloud-core-http-1.94.0.jar:1.94.0] at
com.google.cloud.http.CensusHttpModule$CensusHttpRequestInitializer.initialize(CensusHttpModule.java:109)
~[google-cloud-core-http-1.94.0.jar:1.94.0] at
com.google.api.client.http.HttpRequestFactory.buildRequest(HttpRequestFactory.java:88)
~[google-http-client-1.38.0.jar:1.38.0]
Not sure what's going on here. Any pointers?

UTF-8 decoding in Spring Boot GET request to static resource

I have this #Bean redirecting requests to my Spring Boot backend.
#Bean
WebMvcConfigurer configurer () {
return new WebMvcConfigurerAdapter() {
#Override
public void addResourceHandlers (ResourceHandlerRegistry registry) {
registry.addResourceHandler("/data/static/images/**")
.addResourceLocations("file:" + System.getProperty("user.dir") + "/static/img/");
}
};
}
It works perfectly for URLs such as:
http://localhost:4200/data/static/images/champion/tiles/Ahri_0.jpg
But not for URLs such as:
http://localhost:4200/data/static/images/champion/tiles/Tahm%20Kench_0.jpg
The image is correctly shown in my Angular2 front end if the champion name does not contain any of: space, ampersand or single quote characters.
I ran a trace level logging debug and made both types of requests -- one with and without a "bad" character. Currently, it seems as if the backend searches for the correct file. However, it turns out that it claims it can't find it. I quintuple checked the file in my insanity, I know it is there and that the path printed is correct.
Here is an example log message:
2018-11-18 05:07:14.496 TRACE 9897 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Testing handler map [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping#4d0bdef0] in DispatcherServlet with name 'dispatcherServlet'
2018-11-18 05:07:14.497 DEBUG 9897 --- [nio-8080-exec-1] o.s.w.s.handler.SimpleUrlHandlerMapping : Matching patterns for request [/data/static/images/champion/tiles/Tahm Kench_0.jpg] are [/data/static/images/**, /**]
2018-11-18 05:07:14.497 DEBUG 9897 --- [nio-8080-exec-1] o.s.w.s.handler.SimpleUrlHandlerMapping : URI Template variables for request [/data/static/images/champion/tiles/Tahm Kench_0.jpg] are {}
2018-11-18 05:07:14.497 DEBUG 9897 --- [nio-8080-exec-1] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapping [/data/static/images/champion/tiles/Tahm Kench_0.jpg] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[URL [file:/home/nuradin/Development/Java/riot-api-interface/static/img/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver#5b5b59]]] and 1 interceptor
2018-11-18 05:07:14.497 TRACE 9897 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Testing handler adapter [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#132f4851]
2018-11-18 05:07:14.497 TRACE 9897 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Testing handler adapter [org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter#5b8d72dc]
2018-11-18 05:07:14.498 DEBUG 9897 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Last-Modified value for [/data/static/images/champion/tiles/Tahm%20Kench_0.jpg] is: -1
2018-11-18 05:07:14.498 DEBUG 9897 --- [on(7)-127.0.0.1] sun.rmi.transport.tcp : RMI TCP Connection(7)-127.0.0.1: (port 34127) op = 82
2018-11-18 05:07:14.498 TRACE 9897 --- [nio-8080-exec-1] o.s.w.s.resource.PathResourceResolver : Resolving resource for request path "champion/tiles/Tahm Kench_0.jpg"
2018-11-18 05:07:14.498 TRACE 9897 --- [nio-8080-exec-1] o.s.w.s.resource.PathResourceResolver : Checking location: URL [file:/home/nuradin/Development/Java/riot-api-interface/static/img/]
EDIT: I've implemented the class in the accepted answer, and it's definitely working because I printed the resource name (after encoding the characters I had trouble with.) However, the result is still a 404 error.
Log is below. The champion/Aatrox2Epng bit is due to a println statement in the method #slimane posted below.
2018-11-18 05:56:40.509 TRACE 12951 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Testing handler adapter [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#465ac973]
2018-11-18 05:56:40.509 TRACE 12951 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Testing handler adapter [org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter#37df7ae5]
2018-11-18 05:56:40.509 DEBUG 12951 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Last-Modified value for [/data/static/images/champion/Aatrox.png] is: -1
2018-11-18 05:56:40.509 TRACE 12951 --- [nio-8080-exec-1] o.s.w.s.r.CachingResourceResolver : Resolving resource for request path "champion/Aatrox.png"
2018-11-18 05:56:40.510 TRACE 12951 --- [nio-8080-exec-1] o.e.r.c.EncodedPathResourceResolver : Resolving resource for request path "champion/Aatrox.png"
2018-11-18 05:56:40.510 TRACE 12951 --- [nio-8080-exec-1] o.e.r.c.EncodedPathResourceResolver : Checking location: URL [file:/home/nuradin/Development/Java/riot-api-interface/static/8.23.1/img]
champion/Aatrox2Epng
2018-11-18 05:56:40.511 TRACE 12951 --- [nio-8080-exec-1] o.e.r.c.EncodedPathResourceResolver : No match for location: URL [file:/home/nuradin/Development/Java/riot-api-interface/static/8.23.1/img]
2018-11-18 05:56:40.511 TRACE 12951 --- [nio-8080-exec-1] o.s.w.s.r.ResourceHttpRequestHandler : No matching resource found - returning 404
2018-11-18 05:56:40.511 DEBUG 12951 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
another EDIT -- sorry for the confusion, I changed the path because I thought paths with periods would work now.
#Bean
WebMvcConfigurer configurer () {
return new WebMvcConfigurerAdapter() {
#Override
public void addResourceHandlers (ResourceHandlerRegistry registry) {
registry.addResourceHandler("/data/static/images/**")
.addResourceLocations("file:" + System.getProperty("user.dir") + "/static/8.23.1/img")
.resourceChain(true)
.addResolver(encodedPathResourceResolver());
}
};
}
define your own PathResourceResolver as below:
import org.springframework.core.io.Resource;
import org.springframework.web.servlet.resource.PathResourceResolver;
import org.springframework.web.servlet.resource.ResourceResolver;
import java.io.IOException;
public class CustomPathResourceResolver extends PathResourceResolver implements ResourceResolver {
#Override
protected Resource getResource(String resourcePath, Resource location) throws IOException {
//fixes problems with whitespaces in url
resourcePath = resourcePath.replace(" ","%20");
return super.getResource(resourcePath, location);
}
}
and then register it in your configuration:
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/data/static/images/**")
.addResourceLocations("file:" + System.getProperty("user.dir") + "/static/img/")
.resourceChain(true)
.addResolver(new CustomPathResourceResolver())
;
}
This fixed the Problem for me. Somehow the path was encoded 2 times.
This removes all URI encoding not just whitespaces.
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/resources/**")
.addResourceLocations("file:/Q:/Technik/")
.resourceChain(true)
.addResolver(new CustomPathResourceResolver());
}
public class CustomPathResourceResolver extends PathResourceResolver implements ResourceResolver {
#Override
protected Resource getResource(String resourcePath, Resource location) throws IOException {
//fixes problems with special chars in url
log.debug(resourcePath);
resourcePath = UriUtils.decode(resourcePath, "UTF-8");
log.debug(resourcePath);
resourcePath = UriUtils.decode(resourcePath, "UTF-8");
log.debug(resourcePath);
return super.getResource(resourcePath, location);
}
}

What is wrong in this Spring Boot Swagger configuration to obtain the documentation of my REST API? Why I can't access to the documentation?

I am working on a Spring Boot application and I am trying to configure Swagger to automatically generate my REST service documentation.
I am following this tutorial: http://www.baeldung.com/swagger-2-documentation-for-spring-rest-api
But I am finding some difficulties to do it.
So basically I have created the following Java configuration class into my Spring Boot project:
#Configuration
#EnableSwagger2
public class Config {
#Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
I think that this is the minimal configuration to generate the Swagger documentation of all my REST service. I have put this configuration class at the same level of the Application class (the class that contains the main() method that start my project). I think that the Config class is ok because I have tryed to put a brack point into the api() method and I can see that it enter in this method at the project startup so I think that this configuration is loaded.
Then in the previous tutorial it say that to verify that the my REST API Swagger documentation is generated I have to perform a GET request to this URL: http://localhost:8080/spring-security-rest/api/v2/api-docs
I think that this URL is related to the example project and not to my project.
So I tried to use: http://localhost:8080/api-docs
But doing in this way I am obtaining this error message:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sat Jan 14 16:05:23 CET 2017
There was an unexpected error (type=Not Found, status=404).
No message available
And in the IDE console I have this message:
[DEBUG] 2017-01-14 16:40:05 [org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:865)] [http-nio-8080-exec-4] DispatcherServlet - DispatcherServlet with name 'dispatcherServlet' processing GET request for [/swagger-ui.html]
[DEBUG] 2017-01-14 16:40:05 [org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:310)] [http-nio-8080-exec-4] RequestMappingHandlerMapping - Looking up handler method for path /swagger-ui.html
[DEBUG] 2017-01-14 16:40:05 [org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:320)] [http-nio-8080-exec-4] RequestMappingHandlerMapping - Did not find handler method for [/swagger-ui.html]
[DEBUG] 2017-01-14 16:40:05 [org.springframework.web.servlet.handler.AbstractUrlHandlerMapping.lookupHandler(AbstractUrlHandlerMapping.java:190)] [http-nio-8080-exec-4] SimpleUrlHandlerMapping - Matching patterns for request [/swagger-ui.html] are [/**]
[DEBUG] 2017-01-14 16:40:05 [org.springframework.web.servlet.handler.AbstractUrlHandlerMapping.lookupHandler(AbstractUrlHandlerMapping.java:219)] [http-nio-8080-exec-4] SimpleUrlHandlerMapping - URI Template variables for request [/swagger-ui.html] are {}
[DEBUG] 2017-01-14 16:40:05 [org.springframework.web.servlet.handler.AbstractUrlHandlerMapping.getHandlerInternal(AbstractUrlHandlerMapping.java:140)] [http-nio-8080-exec-4] SimpleUrlHandlerMapping - Mapping [/swagger-ui.html] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[ServletContext resource [/], class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver#4e671ef]]] and 1 interceptor
[DEBUG] 2017-01-14 16:40:05 [org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:951)] [http-nio-8080-exec-4] DispatcherServlet - Last-Modified value for [/swagger-ui.html] is: -1
[DEBUG] 2017-01-14 16:40:05 [org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1044)] [http-nio-8080-exec-4] DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
[DEBUG] 2017-01-14 16:40:05 [org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1000)] [http-nio-8080-exec-4] DispatcherServlet - Successfully completed request
[DEBUG] 2017-01-14 16:40:05 [org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:865)] [http-nio-8080-exec-4] DispatcherServlet - DispatcherServlet with name 'dispatcherServlet' processing GET request for [/error]
[DEBUG] 2017-01-14 16:40:05 [org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:310)] [http-nio-8080-exec-4] RequestMappingHandlerMapping - Looking up handler method for path /error
[DEBUG] 2017-01-14 16:40:05 [org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:317)] [http-nio-8080-exec-4] RequestMappingHandlerMapping - Returning handler method [public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)]
[DEBUG] 2017-01-14 16:40:05 [org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:951)] [http-nio-8080-exec-4] DispatcherServlet - Last-Modified value for [/error] is: -1
[DEBUG] 2017-01-14 16:40:05 [org.springframework.web.servlet.view.ContentNegotiatingViewResolver.getMediaTypes(ContentNegotiatingViewResolver.java:263)] [http-nio-8080-exec-4] ContentNegotiatingViewResolver - Requested media types are [text/html, text/html;q=0.8] based on Accept header types and producible media types [text/html])
[DEBUG] 2017-01-14 16:40:05 [org.springframework.web.servlet.view.BeanNameViewResolver.resolveViewName(BeanNameViewResolver.java:74)] [http-nio-8080-exec-4] BeanNameViewResolver - No matching bean found for view name 'error.html'
[DEBUG] 2017-01-14 16:40:05 [org.springframework.web.servlet.view.ContentNegotiatingViewResolver.getBestView(ContentNegotiatingViewResolver.java:338)] [http-nio-8080-exec-4] ContentNegotiatingViewResolver - Returning [org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$SpelView#50e8ed74] based on requested media type 'text/html'
[DEBUG] 2017-01-14 16:40:05 [org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1251)] [http-nio-8080-exec-4] DispatcherServlet - Rendering view [org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$SpelView#50e8ed74] in DispatcherServlet with name 'dispatcherServlet'
[DEBUG] 2017-01-14 16:40:05 [org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1000)] [http-nio-8080-exec-4] DispatcherServlet - Successfully completed request
The thing that seems me strange is that I have configured nothing related to the URL for my documentation (I don't know if there is a standard URL).
Some further details that maybe could are important to find a solution.
My REST API are implemented in Spring Boot by Controller class that handles request like this:
package com.myapp.controller.room;
#RestController
#RequestMapping("/Room")
public class RoomController {
private static final Logger log = LoggerFactory.getLogger(RoomController.class);
#Autowired
private RoomService roomService;
#Autowired
private RoomMediaService roomMediaService;
public RoomController(){
log.debug("RoomController init");
}
/**
* Ritorna la tipologia di stanza associata ad una stanza
* #param id dellla stanza di cui si intende reperire le informazioni relative alla sua tipologia
* #return RoomTipologyDTO contenente le informazioni relative alla tipologia di stanza
* #throws DataAccessException
*/
#RequestMapping(value = "/{id}/RoomTipology",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<RoomTipologyDTO> getRoomTipologyByRoomId(#PathVariable Long id) throws DataAccessException{
log.debug("getRoomTipologyByRoomId START");
RoomTipologyDTO result = roomService.getRoomTipologyByRoom(id);
log.debug("getRoomTipologyByRoomId END");
return ResponseEntity.ok(result);
}
....................................................................
....................................................................
....................................................................
}
This is the Application class that contains the main() method that start my application:
#SpringBootApplication
#EntityScan("com.betrivius.domain")
#ComponentScan(lazyInit = true)
#EnableAutoConfiguration
public class Application {
private static final Logger log = LoggerFactory.getLogger(Application.class);
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
//context.close();
log.info("Let's inspect the beans provided by Spring Boot:");
String[] beanNames = context.getBeanDefinitionNames();
Arrays.sort(beanNames);
log.info("_______________________________________________________");
for (String beanName : beanNames) {
log.info(beanName);
}
log.info("main() END");
}
}
An example of an URL of one o my service is something like this:
http://localhost:8080/RoomRate/1/RoomRateOptionList
So what is the correct URL to generate my Swagger documentation? Or what am I missing? How can I fix this issue?
Try accessing swagger docs on http://localhost:8080/v2/api-docs. it should work. i think you are using version 2 of swagger documentation.

HttpRequestMethodNotSupportedException: Request method 'POST' not supported

Creating a unit test with MockMvc I am running into:
HttpRequestMethodNotSupportedException: Request method 'POST' not supported
Which causes the test case to fail expecting a '200' but getting a '405'. Some of the additional things you may see in the Junit are for Spring Rest Docs and can be ignored. Such as the #Rule or the .andDo() on the mockMvc Call. I have followed the following documentation Spring Rest Docs - Path Parameter and cannot seem to get it working.
Here is the Controller:
#RestController("/transferObjects")
public class TransferObjectController {
#RequestMapping(method=RequestMethod.GET, produces="application/json")
public List<TransferObject> getTransferObjects(){
// do some magic
return null;
}
#RequestMapping(value = "/{name}", method=RequestMethod.POST)
#ResponseStatus(HttpStatus.OK)
public void addTransferObject(#PathVariable("name")String name){
// do magic
}
#RequestMapping(value = "/{name}", method=RequestMethod.DELETE)
#ResponseStatus(HttpStatus.OK)
public void deleteTransferObject(#PathVariable("name")String name){
// do magic
}
Here is the Junit Class:
public class TransferObjectControllerTest {
#Rule
public RestDocumentation restDocumentation = new RestDocumentation("target/generated-snippets");
private MockMvc mockMvc;
#Before
public void setUp() throws Exception {
this.mockMvc = MockMvcBuilders.standaloneSetup(new TransferObjectController())
.apply(documentationConfiguration(this.restDocumentation))
.build();
}
#Test
public void testAddTransferObject() throws Exception {
this.mockMvc.perform(post("/transferObjects/{name}", "hi"))
.andExpect(status().isOk()).andDo(document("transferObject",
pathParameters(
parameterWithName("name").description("The name of the new Transfer Object to be created."))));
}
And here is the console while running the test:
11:20:48.148 [main] INFO o.s.w.s.m.m.a.RequestMappingHandlerAdapter - Looking for #ControllerAdvice: org.springframework.test.web.servlet.setup.StubWebApplicationContext#5ab785fe
11:20:48.205 [main] DEBUG o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Looking for exception mappings: org.springframework.test.web.servlet.setup.StubWebApplicationContext#5ab785fe
11:20:48.283 [main] DEBUG o.s.t.w.s.TestDispatcherServlet - Initializing servlet ''
11:20:48.307 [main] DEBUG o.s.w.c.s.StandardServletEnvironment - Adding [servletConfigInitParams] PropertySource with lowest search precedence
11:20:48.307 [main] DEBUG o.s.w.c.s.StandardServletEnvironment - Adding [servletContextInitParams] PropertySource with lowest search precedence
11:20:48.312 [main] DEBUG o.s.w.c.s.StandardServletEnvironment - Adding [systemProperties] PropertySource with lowest search precedence
11:20:48.312 [main] DEBUG o.s.w.c.s.StandardServletEnvironment - Adding [systemEnvironment] PropertySource with lowest search precedence
11:20:48.313 [main] DEBUG o.s.w.c.s.StandardServletEnvironment - Initialized StandardServletEnvironment with PropertySources [servletConfigInitParams,servletContextInitParams,systemProperties,systemEnvironment]
11:20:48.313 [main] INFO o.s.mock.web.MockServletContext - Initializing Spring FrameworkServlet ''
11:20:48.313 [main] INFO o.s.t.w.s.TestDispatcherServlet - FrameworkServlet '': initialization started
11:20:48.318 [main] DEBUG o.s.t.w.s.TestDispatcherServlet - Unable to locate MultipartResolver with name 'multipartResolver': no multipart request handling provided
11:20:48.318 [main] DEBUG o.s.t.w.s.TestDispatcherServlet - Using LocaleResolver [org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver#63238bf4]
11:20:48.318 [main] DEBUG o.s.t.w.s.TestDispatcherServlet - Using ThemeResolver [org.springframework.web.servlet.theme.FixedThemeResolver#32b97305]
11:20:48.319 [main] DEBUG o.s.t.w.s.TestDispatcherServlet - Using RequestToViewNameTranslator [org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator#2d2e6747]
11:20:48.319 [main] DEBUG o.s.t.w.s.TestDispatcherServlet - Using FlashMapManager [org.springframework.web.servlet.support.SessionFlashMapManager#417e7d7d]
11:20:48.319 [main] DEBUG o.s.t.w.s.TestDispatcherServlet - Published WebApplicationContext of servlet '' as ServletContext attribute with name [org.springframework.web.servlet.FrameworkServlet.CONTEXT.]
11:20:48.319 [main] INFO o.s.t.w.s.TestDispatcherServlet - FrameworkServlet '': initialization completed in 6 ms
11:20:48.319 [main] DEBUG o.s.t.w.s.TestDispatcherServlet - Servlet '' configured successfully
11:20:48.361 [main] DEBUG o.s.t.w.s.TestDispatcherServlet - DispatcherServlet with name '' processing POST request for [/transferObjects/hi]
11:20:48.364 [main] DEBUG o.s.t.w.s.s.StandaloneMockMvcBuilder$StaticRequestMappingHandlerMapping - Looking up handler method for path /transferObjects/hi
11:20:48.368 [main] DEBUG o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Resolving exception from handler [null]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported
11:20:48.369 [main] DEBUG o.s.w.s.m.a.ResponseStatusExceptionResolver - Resolving exception from handler [null]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported
11:20:48.369 [main] DEBUG o.s.w.s.m.s.DefaultHandlerExceptionResolver - Resolving exception from handler [null]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported
11:20:48.369 [main] WARN o.s.web.servlet.PageNotFound - Request method 'POST' not supported
11:20:48.370 [main] DEBUG o.s.t.w.s.TestDispatcherServlet - Null ModelAndView returned to DispatcherServlet with name '': assuming HandlerAdapter completed request handling
11:20:48.370 [main] DEBUG o.s.t.w.s.TestDispatcherServlet - Successfully completed request
It appears I was able to fix this problem.
Taking a closer look at the console while running the test I noticed:
Mapped "{[],methods=[GET],produces=[application/json]}" onto public java.util.List<common.to.TransferObject> sf.common.controller.TransferObjectController.getTransferObjects()
Mapped "{[/{name}],methods=[POST]}" onto public void sf.common.controller.TransferObjectController.addTransferObject(java.lang.String)
Mapped "{[/{name}],methods=[DELETE]}" onto public void sf.common.controller.TransferObjectController.deleteTransferObject(java.lang.String)
this shows that it is mapping the controller request mappings to the specific RequestMapping the method holds. #RestController("/transferObjects") is not actually defining the mapping as a parent. For me to do that I must include #RequestMapping("/transferObjects") underneath the #RestController.
So by changing the parent mapping I can now use the following test case:
#Test
public void testAddTransferObject() throws Exception {
this.mockMvc.perform(post("/transferObjects/{name}", "hi"))
.andExpect(status().isOk()).andDo(document("transferObject",
pathParameters(
parameterWithName("name").description("The name of the new Transfer Object to be created."))));
}

Resources