M
M
MaxLich2019-08-02 12:45:06
Java
MaxLich, 2019-08-02 12:45:06

Why do I get null URL errors when accessing swagger on tomcat? How to fix it?

Hello. We develop webrest apion java. We use springit sping bootfor auto-configuration and local launch of applications. We also use swaggerit to work with our REST APIs. Deploy it in Apache Tomcat 9.0.x.
Library versions (from pom.xml):

<properties>
         <springframework.boot>2.1.2.RELEASE</springframework.boot>
        <springframework.version>5.1.4.RELEASE</springframework.version>

        <swagger.version>2.9.2</swagger.version>
        <java.version>11</java.version>

        <tomcat.version>9.0.14</tomcat.version>
        <postgresql.jdbc.version>42.2.5</postgresql.jdbc.version>

      </properties>

There used to be one application, now it has been divided into three. Errors are of this type:
[ERROR] 2019-08-01 10:07:48.791 [http-nio-8080-exec-7] RestExceptionHandler - No handler found for GET /my-project-rest/api/v1/null/swagger-resources/configuration/security

org.springframework.web.servlet.NoHandlerFoundException: No handler found for GET /my-project/api/v1/null/swagger-resources/configuration/security

  at org.springframework.web.servlet.DispatcherServlet.noHandlerFound(DispatcherServlet.java:1252) [spring-webmvc-5.1.4.RELEASE.jar:5.1.4.RELEASE]

So far, I temporarily solved it with a kind of crutch solution ( бинin the main configuration):
@Bean
    public WebMvcConfigurer webMvcConfigurer() {
        return new WebMvcConfigurer() {

            //every controller without @DateTimeFormat
            @Override
            public void addFormatters(FormatterRegistry registry) {
                registry.addConverter(new LocalDateConverter(DATE_FORMAT_RU));
                registry.addConverter(new LocalDateTimeConverter(DATE_TIME_FORMAT_RU));
            }

            //Enable mappingJackson2HttpMessageConverter
            @Override
            public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
                converters.add(stringConverter());
                converters.add(mappingJackson2HttpMessageConverter());
            }

            //Enable matrix variable
            @Override
            public void configurePathMatch(PathMatchConfigurer configurer) {
                UrlPathHelper pathHelper = new UrlPathHelper();
                pathHelper.setRemoveSemicolonContent(false);
                configurer.setUrlPathHelper(pathHelper);
            }

            @Override
            public void addResourceHandlers(ResourceHandlerRegistry registry) {
                registry.addResourceHandler("swagger-ui.html")
                        .addResourceLocations("classpath:/META-INF/resources/swagger-ui.html");

                registry.addResourceHandler("/webjars/**")
                        .addResourceLocations("classpath:/META-INF/resources/webjars/");

                registry.addResourceHandler("/null/swagger-resources/**") // !!!!!
                        .addResourceLocations("classpath:/META-INF/resources/");

/*
                registry.addResourceHandler("swagger-ui.html")
                        .addResourceLocations("classpath:/META-INF/resources/");
                registry.addResourceHandler("/webjars/**")
                        .addResourceLocations("classpath:/META-INF/resources/webjars/");
*/
            }
        };
    }

Exclamation points in the comment marked the code that I added. It used to be like commented out below in this method.
PS I saw information on the network on such a problem, but there were problems at the launch through the spring boot, but here when starting in the tomcat, such errors appear.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question