我有一个 Spring Boot 3.0.3的小玩具示例。
这是我的两个简单类:
package org.crea.water4agrifoodmock;
import org.crea.water4agrifoodmock.controller.ModelMockerController;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
// added this @SpringBootApplication(scanBasePackages = {"org.crea.water4agrifoodmock", "org.crea.water4agrifoodmock.controller"}) but without valid result
@SpringBootApplication
public class Water4AgriFoodMockApplication {
public static void main(String[] args) {
SpringApplication.run(
Water4AgriFoodMockApplication.class, args);
}
}
以及控制器:
package org.crea.water4agrifoodmock.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(name = "/api/v1")
public class ModelMockerController {
@GetMapping(name = "/mock")
public String hello(){
return "Hello";
}
}
最好的做法是将主应用程序类放置在项目的根目录下。这样,它将自动扫描并查找子包中可用的Controller类。例如,以下是在page中工作的包结构:
我得到这个错误消息在 Postman :
我的pom文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.crea</groupId>
<artifactId>water4agrifood-mock</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>water4agrifood-mock</name>
<description>Demo project water4agrifood-mockSpring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
这是运行视图
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.0.3)
2023-02-28T16:17:47.705+01:00 INFO 15268 --- [ main] o.c.w.Water4AgriFoodMockApplication : Starting Water4AgriFoodMockApplication using Java 19.0.1 with PID 15268 (C:\Users\*****\spring-boot-water4agrifood-model-mocker\water4agrifood-mock\target\classes started by ***** in C:\Users\*****\spring-boot-water4agrifood-model-mocker\water4agrifood-mock)
2023-02-28T16:17:47.708+01:00 INFO 15268 --- [ main] o.c.w.Water4AgriFoodMockApplication : No active profile set, falling back to 1 default profile: "default"
2023-02-28T16:17:48.409+01:00 INFO 15268 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2023-02-28T16:17:48.416+01:00 INFO 15268 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2023-02-28T16:17:48.416+01:00 INFO 15268 --- [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.5]
2023-02-28T16:17:48.489+01:00 INFO 15268 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2023-02-28T16:17:48.490+01:00 INFO 15268 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 737 ms
2023-02-28T16:17:48.658+01:00 DEBUG 15268 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : ControllerAdvice beans: 0 @ModelAttribute, 0 @InitBinder, 1 RequestBodyAdvice, 1 ResponseBodyAdvice
2023-02-28T16:17:48.721+01:00 DEBUG 15268 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : 2 mappings in 'requestMappingHandlerMapping'
2023-02-28T16:17:48.733+01:00 DEBUG 15268 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Patterns [/webjars/**, /**] in 'resourceHandlerMapping'
2023-02-28T16:17:48.738+01:00 DEBUG 15268 --- [ main] .m.m.a.ExceptionHandlerExceptionResolver : ControllerAdvice beans: 0 @ExceptionHandler, 1 ResponseBodyAdvice
2023-02-28T16:17:48.792+01:00 INFO 15268 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2023-02-28T16:17:48.799+01:00 INFO 15268 --- [ main] o.c.w.Water4AgriFoodMockApplication : Started Water4AgriFoodMockApplication in 1.427 seconds (process running for 1.927)
2023-02-28T16:17:53.490+01:00 INFO 15268 --- [nio-8080-exec-4] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2023-02-28T16:17:53.490+01:00 INFO 15268 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2023-02-28T16:17:53.490+01:00 DEBUG 15268 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : Detected StandardServletMultipartResolver
2023-02-28T16:17:53.490+01:00 DEBUG 15268 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : Detected AcceptHeaderLocaleResolver
2023-02-28T16:17:53.490+01:00 DEBUG 15268 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : Detected FixedThemeResolver
2023-02-28T16:17:53.490+01:00 DEBUG 15268 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : Detected org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator@6deaf644
2023-02-28T16:17:53.491+01:00 DEBUG 15268 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : Detected org.springframework.web.servlet.support.SessionFlashMapManager@57d613cd
2023-02-28T16:17:53.491+01:00 DEBUG 15268 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : enableLoggingRequestDetails='false': request parameters and headers will be masked to prevent unsafe logging of potentially sensitive data
2023-02-28T16:17:53.491+01:00 INFO 15268 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
2023-02-28T16:17:53.497+01:00 DEBUG 15268 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : GET "/api/v1/mock", parameters={}
2023-02-28T16:17:53.508+01:00 DEBUG 15268 --- [nio-8080-exec-4] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped to ResourceHttpRequestHandler [classpath [META-INF/resources/], classpath [resources/], classpath [static/], classpath [public/], ServletContext [/]]
2023-02-28T16:17:53.515+01:00 DEBUG 15268 --- [nio-8080-exec-4] o.s.w.s.r.ResourceHttpRequestHandler : Resource not found
2023-02-28T16:17:53.516+01:00 DEBUG 15268 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : Completed 404 NOT_FOUND
2023-02-28T16:17:53.520+01:00 DEBUG 15268 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : "ERROR" dispatch for GET "/error", parameters={}
2023-02-28T16:17:53.521+01:00 DEBUG 15268 --- [nio-8080-exec-4] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#error(HttpServletRequest)
2023-02-28T16:17:53.541+01:00 DEBUG 15268 --- [nio-8080-exec-4] o.s.w.s.m.m.a.HttpEntityMethodProcessor : Using 'application/json', given [*/*] and supported [application/json, application/*+json]
2023-02-28T16:17:53.543+01:00 DEBUG 15268 --- [nio-8080-exec-4] o.s.w.s.m.m.a.HttpEntityMethodProcessor : Writing [{timestamp=Tue Feb 28 16:17:53 CET 2023, status=404, error=Not Found, path=/api/v1/mock}]
2023-02-28T16:17:53.571+01:00 DEBUG 15268 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : Exiting from "ERROR" dispatch, status 404
添加logging.level.root=DEBUG
后(仅部分)
javax.management.InstanceNotFoundException: org.springframework.boot:type=Admin,name=SpringApplication
at java.management/com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getMBean(DefaultMBeanServerInterceptor.java:1079) ~[na:na]
at java.management/com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getAttribute(DefaultMBeanServerInterceptor.java:637) ~[na:na]
at java.management/com.sun.jmx.mbeanserver.JmxMBeanServer.getAttribute(JmxMBeanServer.java:679) ~[na:na]
at java.management.rmi/javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1449) ~[na:na]
at java.management.rmi/javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1310) ~[na:na]
at java.management.rmi/javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1405) ~[na:na]
at java.management.rmi/javax.management.remote.rmi.RMIConnectionImpl.getAttribute(RMIConnectionImpl.java:639) ~[na:na]
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:578) ~[na:na]
at java.rmi/sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:360) ~[na:na]
at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:200) ~[na:na]
at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:197) ~[na:na]
at java.base/java.security.AccessController.doPrivileged(AccessController.java:712) ~[na:na]
at java.rmi/sun.rmi.transport.Transport.serviceCall(Transport.java:196) ~[na:na]
at java.rmi/sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:598) ~[na:na]
at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:844) ~[na:na]
at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:721) ~[na:na]
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399) ~[na:na]
at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:720) ~[na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144) ~[na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642) ~[na:na]
at java.base/java.lang.Thread.run(Thread.java:1589) ~[na:na]
2023-02-28T16:44:36.181+01:00 DEBUG 14952 --- [ main] _.s.web.servlet.HandlerMapping.Mappings :
o.s.b.a.w.s.e.BasicErrorController:
{ [/error]}: error(HttpServletRequest)
{ [/error], produces [text/html]}: errorHtml(HttpServletRequest,HttpServletResponse)
1条答案
按热度按时间juud5qan1#
@RequestMapping
和@GetMapping
中的name
属性只是配置Map的名称,与API路径无关,应该使用path
或value
:或