简体   繁体   中英

Spring boot rest controller not mapped

I'm building simple test Rest api, basically when /testing is called it will create an logging object and send that to another api ( seperate project and its running). But whenever going to /test not going through the controller.The weird part is, it was working before. I included logging.level.org.springframework.web = DEBUG in application.properties.Below are the log and my source files.

2019-01-17 15:53:17.475  INFO 7981 --- [           main] m.c.c.s.t.TestProjectApplication         : No active profile set, falling back to default profiles: default
2019-01-17 15:53:17.975  INFO 7981 --- [           main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=34ed4a34-6e50-3cb5-ac90-9466e43258a8
2019-01-17 15:53:18.025  INFO 7981 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$dcc06eb1] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-01-17 15:53:18.286  INFO 7981 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 9010 (http)
2019-01-17 15:53:18.315  INFO 7981 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-01-17 15:53:18.315  INFO 7981 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.14]
2019-01-17 15:53:18.320  INFO 7981 --- [           main] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/java/packages/lib:/usr/lib64:/lib64:/lib:/usr/lib]
2019-01-17 15:53:18.459  INFO 7981 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-01-17 15:53:18.459 DEBUG 7981 --- [           main] o.s.web.context.ContextLoader            : Published root WebApplicationContext as ServletContext attribute with name [org.springframework.web.context.WebApplicationContext.ROOT]
2019-01-17 15:53:18.459  INFO 7981 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 975 ms
2019-01-17 15:53:18.514  WARN 7981 --- [           main] o.s.c.n.a.ArchaiusAutoConfiguration      : No spring.application.name found, defaulting to 'application'
2019-01-17 15:53:18.516  WARN 7981 --- [           main] c.n.c.sources.URLConfigurationSource     : No URLs will be polled as dynamic configuration sources.
2019-01-17 15:53:18.516  INFO 7981 --- [           main] c.n.c.sources.URLConfigurationSource     : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
2019-01-17 15:53:18.518  WARN 7981 --- [           main] c.n.c.sources.URLConfigurationSource     : No URLs will be polled as dynamic configuration sources.
2019-01-17 15:53:18.518  INFO 7981 --- [           main] c.n.c.sources.URLConfigurationSource     : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
2019-01-17 15:53:18.573 DEBUG 7981 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Patterns [/**/favicon.ico] in 'faviconHandlerMapping'
2019-01-17 15:53:18.669  INFO 7981 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-01-17 15:53:18.676 DEBUG 7981 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : ControllerAdvice beans: 0 @ModelAttribute, 0 @InitBinder, 1 RequestBodyAdvice, 1 ResponseBodyAdvice
2019-01-17 15:53:18.705 DEBUG 7981 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : 2 mappings in 'requestMappingHandlerMapping'
2019-01-17 15:53:18.713 DEBUG 7981 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Patterns [/webjars/**, /**] in 'resourceHandlerMapping'
2019-01-17 15:53:18.721 DEBUG 7981 --- [           main] .m.m.a.ExceptionHandlerExceptionResolver : ControllerAdvice beans: 0 @ExceptionHandler, 1 ResponseBodyAdvice
2019-01-17 15:53:18.871  INFO 7981 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 9010 (http) with context path ''
2019-01-17 15:53:18.874  INFO 7981 --- [           main] m.c.c.s.t.TestProjectApplication         : Started TestProjectApplication in 1.976 seconds (JVM running for 2.504)
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
2019-01-17 15:53:26.870  INFO 7981 --- [nio-9010-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2019-01-17 15:53:26.870  INFO 7981 --- [nio-9010-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2019-01-17 15:53:26.870 DEBUG 7981 --- [nio-9010-exec-1] o.s.web.servlet.DispatcherServlet        : Detected StandardServletMultipartResolver
2019-01-17 15:53:26.875 DEBUG 7981 --- [nio-9010-exec-1] o.s.web.servlet.DispatcherServlet        : enableLoggingRequestDetails='false': request parameters and headers will be masked to prevent unsafe logging of potentially sensitive data
2019-01-17 15:53:26.875  INFO 7981 --- [nio-9010-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 5 ms
2019-01-17 15:53:26.881 DEBUG 7981 --- [nio-9010-exec-1] o.s.web.servlet.DispatcherServlet        : GET "/testing", parameters={}
2019-01-17 15:53:26.886 DEBUG 7981 --- [nio-9010-exec-1] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped to ResourceHttpRequestHandler ["classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/", "/"]
2019-01-17 15:53:26.887 DEBUG 7981 --- [nio-9010-exec-1] o.s.w.s.r.ResourceHttpRequestHandler     : Resource not found
2019-01-17 15:53:26.887 DEBUG 7981 --- [nio-9010-exec-1] o.s.web.servlet.DispatcherServlet        : Completed 404 NOT_FOUND
2019-01-17 15:53:26.890 DEBUG 7981 --- [nio-9010-exec-1] o.s.web.servlet.DispatcherServlet        : "ERROR" dispatch for GET "/error", parameters={}
2019-01-17 15:53:26.894 DEBUG 7981 --- [nio-9010-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2019-01-17 15:53:26.923 DEBUG 7981 --- [nio-9010-exec-1] o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, text/html;q=0.8]
2019-01-17 15:53:26.929 DEBUG 7981 --- [nio-9010-exec-1] o.s.web.servlet.DispatcherServlet        : Exiting from "ERROR" dispatch, status 404

TestProjectApplication.java

package my.com.cybersolution.skb.testproject;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;

import my.com.cybersolution.skb.elogginglibrary.filter.EloggingFilter;

@SpringBootApplication
@ComponentScan(basePackages = {"my.com.cybersolution.skb.elogginglibrary.filter.EloggingFilter"})
public class TestProjectApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestProjectApplication.class, args);
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
    }

    @Bean
    public FilterRegistrationBean<EloggingFilter> loggingFilter(){
        FilterRegistrationBean<EloggingFilter> registrationBean = new FilterRegistrationBean<>();

        registrationBean.setFilter(new EloggingFilter());
        //registrationBean.addUrlPatterns("/users/*");

        return registrationBean;    
    }

}

TestController.java

package my.com.cybersolution.skb.testproject.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;
import my.com.cybersolution.skb.testproject.client.TestClient;
import my.com.cybersolution.skb.testproject.object.LoggingObject;


@RestController
@RequestMapping("testing")
@EnableFeignClients(basePackages = {"my.com.cybersolution.skb.testproject.client"})
@CrossOrigin
public class TestController {
    @Autowired
    private TestClient testClient;

    @GetMapping
    public String getLoggingList() {
        LoggingObject loggingObject = new LoggingObject();
        loggingObject.setMethod("GET");
        loggingObject.setUrl("test.org");
        loggingObject.setLogDate(new Date());

        testClient.sendMessage(loggingObject);

        return "test success";
    }

    @PostMapping("{id}")
    public String returnMe(@PathVariable("id") String id) {
        return id;

    }
}

TestClient.java

package my.com.cybersolution.skb.testproject.client;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import my.com.cybersolution.skb.testproject.object.LoggingObject;

@FeignClient(name = "eloggingClient", url = "localhost:9090")
@Component
public interface TestClient {
    @PostMapping("/logging")
    public boolean sendMessage(@RequestBody LoggingObject logging);
}

Your RestController isn't called because You added @ComponentScan annotation with fixed basePackages to your TestProjectApplication.java which doesn't include package with your controller. It means that Spring will never scan your controller package and it can't register any endpoints.

To fix that just simply remove @ComponentScan from your TestProjectApplication.java , default behaviour is scanning all packages, so your controller will be again reachable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM