启动应用程序,即使我的服务组件在spring上下文中显示为已注册的bean,它的url也没有被注册。基本上,要注册具有方法级别@RequestMapping
注释的组件,我需要定义类级别请求映射。然后它的spring上下文能够注册url处理程序。
我的目录结构如下;
├── pom.xml
├── src
│ ├── main
│ │ ├── java
│ │ │ └── com
│ │ │ └── awesomecrypto
│ │ │ └── backend
│ │ │ ├── SpringBootApp.java
│ │ │ ├── entity
│ │ │ │ └── MarketData.java
│ │ │ ├── repository
│ │ │ │ └── MarketDataRepository.java
│ │ │ └── service
│ │ │ └── MarketDataService.java
│ │ └── resources
SpringBootApp.java
package com.awesomecrypto.backend;
// import statements.
@SpringBootApplication
public class SpringBootApp {
public static void main(String[] args) {
ApplicationContext applicationContext = SpringApplication.run(SpringBootApp.class, args);
for (String name: applicationContext.getBeanDefinitionNames()) {
System.out.println(name);
}
}
}
MarketDataService
package com.awesomecrypto.backend.service;
// import statements here.
@Component
public class MarketDataService {
@Autowired
private MarketDataRepository marketDataRepository;
@GetMapping("/marketData")
@ResponseBody
public String getMarketData() {
return "foobar";
}
}
如果没有在@RequestMapping
级别定义MarketDataService
,则没有任何网址处理程序注册为“/ marketData”网址。
2018-05-28 15:15:11.357 INFO 30618 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2018-05-28 15:15:11.357 INFO 30618 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1295 ms
2018-05-28 15:15:11.460 INFO 30618 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/]
2018-05-28 15:15:11.465 INFO 30618 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-05-28 15:15:11.465 INFO 30618 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-05-28 15:15:11.465 INFO 30618 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-05-28 15:15:11.465 INFO 30618 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2018-05-28 15:15:11.601 INFO 30618 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-05-28 15:15:11.813 INFO 30618 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@6c65519: startup date [Mon May 28 15:15:10 PDT 2018]; root of context hierarchy
2018-05-28 15:15:11.915 INFO 30618 --- [ main] 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.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-05-28 15:15:11.916 INFO 30618 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-05-28 15:15:11.939 INFO 30618 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-05-28 15:15:11.939 INFO 30618 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
另一方面,marketDataService
在spring上下文中注册,这是没有意义的。以下是相关的getBeanDefinitionNames
日志。
org.springframework.context.event.internalEventListenerFactory
springBootApp
org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory
marketDataRepository
marketDataService
org.springframework.boot.autoconfigure.AutoConfigurationPackages
org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration
org.springframework.boot.autoconfigure.condition.BeanTypeRegistry
现在如果我在@RequestMapping("/test")
之上添加MarketDataService
,那么spring会注册一个url处理程序。
2018-05-28 15:20:31.320 INFO 31536 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/test/marketData],methods=[GET]}" onto public java.lang.String com.awesomecrypto.backend.service.MarketDataService.getMarketData()
我正在运行应用程序;
mvn clean package
mvn spring-boot:run
网络上的所有示例都没有提到这一点,看起来它不需要类级别的RequestMapping,但我无法做到。我感谢您的帮助。谢谢!
答案 0 :(得分:3)
您的@GetMapping
注释被忽略,因为@Controller
课程顶部没有MarketDataService
类型的注释。
在您的情况下,您应该使用@RestController(其本身注释为@Controller
和@ResponseBody
):
@RestController
public class MarketDataService {
@Autowired
private MarketDataRepository marketDataRepository;
@GetMapping("/marketData")
public String getMarketData() {
return "foobar";
}
}
@RequestMapping
在类上的用法是将给定参数以及给定路径参数传递给同一类中的子映射。
例如,在类@RequestMapping("/api")
上方添加MarketDataService
意味着触发getMarketData()
方法的路径为/api/marketData
而不是/marketData
同样,如果您添加了@RequestMapping(value = "/api", produces = "application/json")
,则会将produces = "application/json"
参数传递给@GetMapping("/marketData")
。
当控制器用于CRUD给定资源时,我经常看到@RequestMapping
在类上使用,例如:
@RestController
@RequestMapping("/marketData")
public class MarketDataService {
@Autowired
private MarketDataRepository marketDataRepository;
@PostMapping("")
public String createMarketData() {
// ...
}
@GetMapping("")
public String getAllMarketData() {
// ...
}
@GetMapping("/{id}")
public String getMarketDataById(@PathVariable Long id) {
// ...
}
@DeleteMapping("")
public String deleteAllMarketData() {
// ...
}
@DeleteMapping("/{id}")
public String deleteMarketDataById(@PathVariable Long id) {
// ...
}
// ...
}
答案 1 :(得分:2)
您在课程级别不需要RequestMapping。将您的代码更改为此代码,它将对您有用。
@RestController
public class MarketDataService {
@Autowired
private MarketDataRepository marketDataRepository;
@GetMapping("/marketData")
public String getMarketData() {
return "foobar";
}
}
我建议您阅读有关Spring MVC架构的更多信息。
https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc
此外,您还可以阅读RequestMapping,RestController。
的javaDocs