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? - spring

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.

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.

Prevent DispatcherServlet from logging endpoint in Spring Boot Actuator

I've turned off the Logging for an actuator endpoint, but DispatcherServlet (and RequestReponseBodyMethodProcessor) still log the mapping and response.
How can i prevent this logging for just this one endpoint? I've already turned them off
management.logging.level.org.springframework.boot.actuate.health=OFF
logging.level.org.springframework.boot.actuate.health=OFF
org.springframework.boot.actuate.health.Logger=OFF
but it still comes like this:
2020-05-06 17:14:01.545 DEBUG 58588 --- [nio-9095-exec-5] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/health]
2020-05-06 17:14:01.552 DEBUG 58588 --- [nio-9095-exec-5] o.s.web.servlet.DispatcherServlet : Last-Modified value for [/health] is: -1
2020-05-06 17:14:01.848 DEBUG 58588 --- [nio-9095-exec-5] m.m.a.RequestResponseBodyMethodProcessor : Written [UP {}] as "application/vnd.spring-boot.actuator.v1+json" using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter#144409aa]
2020-05-06 17:14:01.849 DEBUG 58588 --- [nio-9095-exec-5] o.s.web.servlet.DispatcherServlet : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2020-05-06 17:14:01.849 DEBUG 58588 --- [nio-9095-exec-5] o.s.web.servlet.DispatcherServlet : Successfully completed request
Just set in application.properties log level to WARN
logging.level.org.springframework.web.servlet.DispatcherServlet=WARN
logging.level.org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor=WARN
please check if this solves the issue
In annotation SpringBootApplication add exclude DispatcherServletAutoConfiguration class in main class.
#SpringBootApplication(exclude = { DispatcherServletAutoConfiguration.class })
reference :
Switch off DispatcherServlet on Spring Boot
If your goal is logging all the incoming HTTP requests, the solution is not enabling DEBUG logging level in the DispatcherServlet.
You can define a bean of type:
org.springframework.web.filter.CommonsRequestLoggingFilter
and override the method:
org.springframework.web.filter.CommonsRequestLoggingFilter.shouldLog(HttpServletRequest)
So you can decide which requests are logged or not.
Obviously, you must set the logging level of the filter to DEBUG:
logging.level.org.springframework.web.filter.CommonsRequestLoggingFilter=DEBUG
More info at Baeldubg site.
Just use the below in the .properties
logging.level.org.springframework.web=WARN
Sorry to necro a thread, but here's the answer I came up with:
Create a bean as below:
package com.package.your.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.CommonsRequestLoggingFilter;
import javax.servlet.http.HttpServletRequest;
#Configuration
public class RequestLoggingFilterConfig {
#Bean
public CommonsRequestLoggingFilter logFilter() {
CommonsRequestLoggingFilter filter = new CommonsRequestLoggingFilter() {
#Override
public void beforeRequest(HttpServletRequest request, String message) {
// Don't log any actuator endpoints
if (!request.getRequestURI().contains("actuator")) {
this.logger.debug(message);
}
}
#Override
public void afterRequest(HttpServletRequest request, String message) {
// Empty because it logs the same content as beforeRequest
}
};
filter.setBeforeMessagePrefix("");
filter.setBeforeMessageSuffix("");
filter.setIncludeClientInfo(true);
filter.setIncludeHeaders(true);
filter.setIncludePayload(true);
filter.setIncludeQueryString(true);
return filter;
}
}
Add the following to applications.properties instead of setting DispatcherServlet to debug:
logging.level.com.package.your.config.RequestLoggingFilterConfig=debug
This works beautifully as intended. Filtering is controlled by the if statement in the beforeRequest method.

How to disable hawt.io authentication?

I used spring boot, hawt.io, camel to test hawt.io dashboard
plugins {
id 'org.springframework.boot' version '1.5.10.RELEASE'
}
repositories {
mavenCentral()
flatDir {
dirs 'lib'
}
}
dependencies {
// Spring actuator, log4j2
compile("org.springframework.boot:spring-boot-starter-log4j2")
//Spring web
compile("org.springframework.boot:spring-boot-starter-web"){
exclude module: "spring-boot-starter-tomcat"
}
compile("org.springframework.boot:spring-boot-starter-jetty")
compile("org.eclipse.jetty:jetty-jaas")
compile("org.eclipse.jetty:jetty-http")
compile("org.springframework.boot:spring-boot-actuator")
//hawtio
compile("io.hawt:hawtio-springboot:1.5.10")
compile("io.hawt:hawtio-core:1.5.10")
and i had disabled authentication via
hawtio.authenticationEnabled=false
Here is the log:
18:00:13.489 [main] DEBUG ConfigManager - Property noCredentials401 is set to value false
18:00:13.490 [main] DEBUG ConfigManager - Property realm is set to value karaf
18:00:13.490 [main] DEBUG ConfigManager - Property role is set to value null
18:00:13.490 [main] DEBUG ConfigManager - Property roles is set to value null
18:00:13.490 [main] DEBUG ConfigManager - Property rolePrincipalClasses is set to value
18:00:13.490 [main] DEBUG ConfigManager - Property authenticationEnabled is set to value false
18:00:13.490 [main] DEBUG ConfigManager - Property noCredentials401 is set to value false
18:00:13.490 [main] DEBUG ConfigManager - Property authenticationContainerDiscoveryClasses is set to value io.hawt.web.tomcat.TomcatAuthenticationContainerDiscovery
18:00:13.490 [main] INFO AuthenticationFilter - Starting hawtio authentication filter, JAAS authentication disabled
18:00:13.500 [main] DEBUG ConfigManager - Property sessionTimeout is set to value 1800
18:00:13.500 [main] INFO LoginServlet - hawtio login is using 1800 sec. HttpSession timeout
When i open url http://localhost:8091/hawtio/index.html, it always be redirected to http://localhost:8091/hawtio/index.html#/login
How can i disable authentication?
According https://github.com/hawtio/hawtio/issues/1963, the issue should be fixed on 6 Dec 2015, but it's still there.
And according those 404 errors, it seems all requests are handled by spring mvc DispatcherServlet, and those servlets registered in HawtioManagementContextConfiguration are not worked as expected.
18:07:52.821 [qtp1016881733-22] DEBUG DispatcherServlet - DispatcherServlet with name 'dispatcherServlet' processing GET request for [/hawtio/keycloak/enabled]
18:07:52.821 [qtp1016881733-22] DEBUG RequestMappingHandlerMapping - Looking up handler method for path /hawtio/keycloak/enabled
18:07:52.821 [qtp1016881733-22] DEBUG RequestMappingHandlerMapping - Did not find handler method for [/hawtio/keycloak/enabled]
18:07:52.821 [qtp1016881733-22] DEBUG SimpleUrlHandlerMapping - Matching patterns for request [/hawtio/keycloak/enabled] are [/hawtio/**, /**]
18:07:52.821 [qtp1016881733-22] DEBUG SimpleUrlHandlerMapping - URI Template variables for request [/hawtio/keycloak/enabled] are {}
18:07:52.822 [qtp1016881733-22] DEBUG SimpleUrlHandlerMapping - Mapping [/hawtio/keycloak/enabled] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[ServletContext resource [/], ServletContext resource [/app/], class path resource [hawtio-static/], class path resource [hawtio-static/app/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver#244d7ca5]]] and 1 interceptor
18:07:52.822 [qtp1016881733-22] DEBUG DispatcherServlet - Last-Modified value for [/hawtio/keycloak/enabled] is: -1
18:07:52.824 [qtp1016881733-22] DEBUG DispatcherServlet - DispatcherServlet with name 'dispatcherServlet' processing GET request for [/error]
18:07:52.824 [qtp1016881733-22] DEBUG RequestMappingHandlerMapping - Looking up handler method for path /error
18:07:52.825 [qtp1016881733-22] DEBUG 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)]
18:07:52.825 [qtp1016881733-22] DEBUG DispatcherServlet - Last-Modified value for [/error] is: -1
18:07:52.833 [qtp1016881733-22] DEBUG ContentNegotiatingViewResolver - Requested media types are [text/html, text/html;q=0.8] based on Accept header types and producible media types [text/html])
18:07:52.833 [qtp1016881733-22] DEBUG BeanNameViewResolver - No matching bean found for view name 'error.html'
18:07:52.838 [qtp1016881733-22] DEBUG ContentNegotiatingViewResolver - Returning [org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$SpelView#150fc7a7] based on requested media type 'text/html'
18:07:52.838 [qtp1016881733-22] DEBUG DispatcherServlet - Rendering view [org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$SpelView#150fc7a7] in DispatcherServlet with name 'dispatcherServlet'
18:07:52.886 [qtp1016881733-22] DEBUG DispatcherServlet - Successfully completed request
18:07:52.887 [qtp1016881733-22] DEBUG DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
18:07:52.887 [qtp1016881733-22] DEBUG DispatcherServlet - Successfully completed request
18:07:52.965 [qtp1016881733-47] DEBUG DispatcherServlet - DispatcherServlet with name 'dispatcherServlet' processing GET request for [/favicon.ico]
18:07:52.965 [qtp1016881733-47] DEBUG SimpleUrlHandlerMapping - Matching patterns for request [/favicon.ico] are [/**/favicon.ico]
18:07:52.965 [qtp1016881733-47] DEBUG SimpleUrlHandlerMapping - URI Template variables for request [/favicon.ico] are {}
18:07:52.965 [qtp1016881733-47] DEBUG SimpleUrlHandlerMapping - Mapping [/favicon.ico] 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/], class path resource []], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver#ec04917]]] and 1 interceptor
18:07:52.965 [qtp1016881733-47] DEBUG DispatcherServlet - Last-Modified value for [/favicon.ico] is: -1
18:07:52.969 [qtp1016881733-47] DEBUG DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
18:07:52.969 [qtp1016881733-47] DEBUG DispatcherServlet - Successfully completed request
Mostly the reason is that you just forget the final required step to use Hawtio with Spring Boot. You need this line in your application.properties:
endpoints.jolokia.sensitive = false
Without this setting Jolokia endpoint always returns 401 for unauthenticated requests, thus causing redirects to the login page.
You can also refer to a working example of unauthenticated Hawtio with Spring Boot here:
https://github.com/hawtio/hawtio/tree/master/hawtio-sample-springboot
By the way, Hawtio 2.0 will be released very soon.
Finally, i found the root cause is that
management.port != server.port
I referenced this one SpringBootCamelStarter, i also used 8095 for management port and 8091 for server port, and i used the normal server port 8091 to access it, that's the issue, i should use management port 8095 to access hawtio dashboard. here is the clear description:https://github.com/hawtio/hawtio/tree/2.x/examples/springboot

Spring boot do not change HTTP code in case of exception

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'

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