gpt4 book ai didi

java - 不调用 Spring REST API Controller

转载 作者:搜寻专家 更新时间:2023-11-01 01:20:55 25 4
gpt4 key购买 nike

我有以下代码

Controller

@RestController 
@RequestMapping("/")
public class RequestHandler {
@RequestMapping(value = "/demo", method = RequestMethod.POST)
@ResponseBody
public Object showDemo() {

return "Post method";
}


@RequestMapping(value = { "/getdemo" }, method = RequestMethod.GET)
@ResponseBody
public Object showgetDemo() {
System.out.println("hello");
return "get Method";
}
}

我的 pom xml

    <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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Website2015</groupId>
<artifactId>Website2015</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.1.RELEASE</version>
<relativePath />
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>

</build>
</project>

并启动 Spring boot 应用程序:

@Configuration
@EnableWebMvc
@SpringBootApplication
public class AppLauncher {

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

}

private static Class<AppLauncher> applicationClass = AppLauncher.class;

}

服务器日志

    2015-10-21 16:07:59.314  INFO 12892 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2015-10-21 16:07:59.470 INFO 12892 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2015-10-21 16:07:59.471 INFO 12892 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.0.15
2015-10-21 16:07:59.561 INFO 12892 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2015-10-21 16:07:59.561 INFO 12892 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1158 ms
2015-10-21 16:07:59.716 INFO 12892 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2015-10-21 16:07:59.719 INFO 12892 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2015-10-21 16:07:59.921 INFO 12892 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" 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)
2015-10-21 16:07:59.921 INFO 12892 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],methods=[],params=[],headers=[],consumes=[],produces=[text/html],custom=[]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest)
2015-10-21 16:08:00.119 INFO 12892 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@25af5db5: startup date [Wed Oct 21 16:07:58 CEST 2015]; root of context hierarchy
2015-10-21 16:08:00.275 INFO 12892 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2015-10-21 16:08:00.322 INFO 12892 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2015-10-21 16:08:00.323 INFO 12892 --- [ main] c.xx.yy.misc.AppLauncher : Started AppLauncher in 2.16 seconds (JVM running for 2.496)

我正在使用 POSTMAN Rest 客户端。我得到的只是

{ "timestamp": 1445436550585, "status": 404, "error": "Not Found", "message": "No message available", "path": "/get" }

我也无法在服务器日志中找到映射。花了很多时间后我最好的猜测是错误的项目结构。

我创建了新的 Dynamic web 项目并将其转换为 Maven ,当然我尝试使用简单的 java 项目然后将其转换为 maven 。

请给我一些指示,如果需要更多信息,请告诉我,谢谢。

最佳答案

默认的@SpringBootApplication只对应用类的包和所有子包启用组件扫描。由于AppLauncher的包是com.demo.misc,RequestHandler的包是com.demo.controller,组件扫描不会检测到bean类。

解决方法:
如果您使用的是 Spring Boot 1.2.x:

将以下注释添加到您的 TestApp 类:

@ComponentScan({"com.demo.controller", "com.demo.misc"})

或者将 RequestHandler 移动到 com.demo.misc 的子包中,例如com.demo.misc.controller

如果您已经在使用 Spring Boot 1.3.x:

将以下参数添加到您的 @SpringBootApplication 注释中:

@SpringBootApplication(scanBasePackages = {"com.demo.controller", "com.demo.misc"})    

关于java - 不调用 Spring REST API Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33261714/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com