Spring Boot Rest控制器未映射

时间:2019-01-17 08:07:39

标签: java spring rest spring-boot

我正在构建简单的测试Rest api,基本上在调用/ testing时,它将创建一个日志记录对象并将其发送到另一个api(单独的项目及其运行中)。但是每当去/ test时都不会通过控制器。奇怪的是,它之前一直在工作。我在application.properties中包含了logging.level.org.springframework.web = DEBUG。以下是日志和我的源文件。

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);
}

1 个答案:

答案 0 :(得分:0)

未调用您的RestController是因为您在@ComponentScan中添加了带有固定basePackages的{​​{1}}批注,该批注不包含控制器的包装。这意味着Spring将永远不会扫描您的TestProjectApplication.java软件包,并且无法注册任何端点。

要解决此问题,只需将controller中的@ComponentScan删除,默认行为是扫描所有软件包,因此控制器将再次可用。