简体   繁体   English

剩余URL路径未映射(Spring Boot)

[英]Rest URL Path is Not Getting Mapped ( Spring Boot )

Consider, following classes for my problem, 考虑以下问题,

Boot Application java class : 引导应用程序java类:

package com.abb;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication(scanBasePackages = {"com.abb.repositories"})
public class ServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServerApplication.class, args);
    }
}

CrudRepository interface ( it just added here because i feel package might cause this issue ) : CrudRepository接口(它只是在这里添加,因为我觉得程序包可能会导致此问题):

package com.abb.repositories;

import org.springframework.data.repository.CrudRepository;
import com.abb.entities.XYZ;

public interface XYZRepository extends CrudRepository<XYZ, Long> { }

Controller Java Class For rest API: 控制器Java类对于其余API:

package com.abb.controller;

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.web.bind.annotation.RequestBody; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.ResponseBody; 
import org.springframework.web.bind.annotation.RestController;
import com.abb.repositories.XYZRepository;

@RestController 
@RequestMapping("/abc") 
public class ABCController {

     @Autowired
     private XYZRepository xyzRepository;

     @RequestMapping(value = "/test", method = RequestMethod.POST)
     public @ResponseBody String test(@RequestBody int iValue) {
          return "done" + iValue; 
     }
}

Server Logs : 服务器日志:

2017-10-25 22:22:25.579  INFO 4664 --- [  restartedMain] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2017-10-25 22:22:26.291  INFO 4664 --- [  restartedMain] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4521e6e2: startup date [Wed Oct 25 22:22:17 IST 2017]; root of context hierarchy
2017-10-25 22:22:26.470  INFO 4664 --- [  restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-10-25 22:22:26.485  INFO 4664 --- [  restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-10-25 22:22:26.548  INFO 4664 --- [  restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-10-25 22:22:26.548  INFO 4664 --- [  restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-10-25 22:22:26.657  INFO 4664 --- [  restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-10-25 22:22:27.250  INFO 4664 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2017-10-25 22:22:27.344  INFO 4664 --- [  restartedMain] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2017-10-25 22:22:27.470  INFO 4664 --- [  restartedMain] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-10-25 22:22:27.486  INFO 4664 --- [  restartedMain] com.yq.WhyqueueServerApplication         : Started WhyqueueServerApplication in 10.814 seconds (JVM running for 11.612)

The problem is that you are limiting spring boots scanner to only scan the repositories package. 问题是您将Spring Boot扫描仪限制为仅扫描存储库软件包。 No other package will be scanned. 不会扫描其他软件包。 This means it does not pick up your controllers, which are in "com.abb.controllers" package. 这意味着它不会选择“ com.abb.controllers”包中的控制器。

Remove the scanBasePackages attribute, replace the value, or add each individual package that you want scanned. 删除scanBasePackages属性,替换该值,或添加要扫描的每个单独的程序包。

I recomend this: 我建议这样做:

@SpringBootApplication

Or this: 或这个:

@SpringBootApplication(scanBasePackages = {"com.abb"})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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