RestController返回404(Spring Boot 2.3.7)
我已经用RestContoller编写了Spring Boot应用程序。但是,当我在浏览器中击中URL时,我会遇到404错误。但是我不确定我在做什么错。一切似乎都是正确的。我的控制器类看起来像这样:
package com.gtt.wcas.device.root;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DeviceConnectionController {
Logger logger = LoggerFactory.getLogger(DeviceConnectionController.class.getName());
.
.
.
// Lots of variables
.
.
.
@RequestMapping(value = "/deviceConnection", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<String> deviceConnection(@RequestParam(value = "verify", defaultValue = "false") String verify) {
.
.
//Method body.... lots of code...
.
.
return new ResponseEntity<String>(HttpStatus.OK);
}
@RequestMapping(value = "/error", method = RequestMethod.GET)
public ResponseEntity<String> error () {
return new ResponseEntity<String>(HttpStatus.INTERNAL_SERVER_ERROR);
}
.
.
.
// Lots of other helper methods.
.
.
.
}
我的主类是这样的:
package com.gtt.wcas.device.root;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages = "com.gtt.wcas.device.details")
@ConfigurationPropertiesScan(basePackages = "com.gtt.wcas.device.configuration")
@EntityScan(basePackages = "com.gtt.wcas.device.db.jpa")
public class wcasDeviceConnectionApplication {
public static void main(String[] args) {
SpringApplication.run(wcasDeviceConnectionApplication.class, args);
}
}
我的应用程序属性是:
#datasource configurations
spring.datasource.url=MY DB URL
spring.datasource.username=MY DB USERNAME
spring.datasource.password=MY DB PASSWORD
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
# Enabling H2 Console
spring.h2.console.enabled=true
# Custom H2 Console URL
spring.h2.console.path=/h2-console
# DDL generation
spring.jpa.generate-ddl=true
logging.level.ch.qos.logback==DEBUG
spring.devtools.restart.enabled=true
debug=true
当我输入http:// localhost:8080/deviceconnection?verify = truifie = true
上时,我会看到此信息在日志中:
2022-06-16 19:00:00.078 DEBUG 19444 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : GET "/deviceConnection?verify=true", parameters={masked}
2022-06-16 19:00:00.079 DEBUG 19444 --- [nio-8080-exec-4] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped to ResourceHttpRequestHandler [classpath [META-INF/resources/], classpath [resources/], classpath [static/], classpath [public/], ServletContext [/]]
2022-06-16 19:00:00.082 DEBUG 19444 --- [nio-8080-exec-4] o.s.w.s.r.ResourceHttpRequestHandler : Resource not found
2022-06-16 19:00:00.082 DEBUG 19444 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : Completed 404 NOT_FOUND
2022-06-16 19:00:00.082 DEBUG 19444 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : "ERROR" dispatch for GET "/error?verify=true", parameters={masked}
2022-06-16 19:00:00.083 DEBUG 19444 --- [nio-8080-exec-4] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#errorHtml(HttpServletRequest, HttpServletResponse)
2022-06-16 19:00:00.096 DEBUG 19444 --- [nio-8080-exec-4] o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, text/html;q=0.8]
2022-06-16 19:00:00.097 DEBUG 19444 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : Exiting from "ERROR" dispatch, status 404
您能否让我知道我在做什么错。任何帮助都非常感谢。
I have written a Spring Boot application with RestContoller. But when I hit the URL in a browser, I get a 404 error. But i am not sure what I am doing wrong. Everything seems to be all correct. My controller class looks like this:
package com.gtt.wcas.device.root;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DeviceConnectionController {
Logger logger = LoggerFactory.getLogger(DeviceConnectionController.class.getName());
.
.
.
// Lots of variables
.
.
.
@RequestMapping(value = "/deviceConnection", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<String> deviceConnection(@RequestParam(value = "verify", defaultValue = "false") String verify) {
.
.
//Method body.... lots of code...
.
.
return new ResponseEntity<String>(HttpStatus.OK);
}
@RequestMapping(value = "/error", method = RequestMethod.GET)
public ResponseEntity<String> error () {
return new ResponseEntity<String>(HttpStatus.INTERNAL_SERVER_ERROR);
}
.
.
.
// Lots of other helper methods.
.
.
.
}
My main class is like this:
package com.gtt.wcas.device.root;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages = "com.gtt.wcas.device.details")
@ConfigurationPropertiesScan(basePackages = "com.gtt.wcas.device.configuration")
@EntityScan(basePackages = "com.gtt.wcas.device.db.jpa")
public class wcasDeviceConnectionApplication {
public static void main(String[] args) {
SpringApplication.run(wcasDeviceConnectionApplication.class, args);
}
}
My application properties are:
#datasource configurations
spring.datasource.url=MY DB URL
spring.datasource.username=MY DB USERNAME
spring.datasource.password=MY DB PASSWORD
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
# Enabling H2 Console
spring.h2.console.enabled=true
# Custom H2 Console URL
spring.h2.console.path=/h2-console
# DDL generation
spring.jpa.generate-ddl=true
logging.level.ch.qos.logback==DEBUG
spring.devtools.restart.enabled=true
debug=true
And when I enter http://localhost:8080/deviceConnection?verify=true
on the browser, I see this in the logs:
2022-06-16 19:00:00.078 DEBUG 19444 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : GET "/deviceConnection?verify=true", parameters={masked}
2022-06-16 19:00:00.079 DEBUG 19444 --- [nio-8080-exec-4] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped to ResourceHttpRequestHandler [classpath [META-INF/resources/], classpath [resources/], classpath [static/], classpath [public/], ServletContext [/]]
2022-06-16 19:00:00.082 DEBUG 19444 --- [nio-8080-exec-4] o.s.w.s.r.ResourceHttpRequestHandler : Resource not found
2022-06-16 19:00:00.082 DEBUG 19444 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : Completed 404 NOT_FOUND
2022-06-16 19:00:00.082 DEBUG 19444 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : "ERROR" dispatch for GET "/error?verify=true", parameters={masked}
2022-06-16 19:00:00.083 DEBUG 19444 --- [nio-8080-exec-4] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#errorHtml(HttpServletRequest, HttpServletResponse)
2022-06-16 19:00:00.096 DEBUG 19444 --- [nio-8080-exec-4] o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, text/html;q=0.8]
2022-06-16 19:00:00.097 DEBUG 19444 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : Exiting from "ERROR" dispatch, status 404
Can you please let me know what am I doing wrong. Any help is much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的控制器在软件包中
com.gtt.wcas.device.root
,但是您设置了@componentscan(basepackages =“ com.gtt.wcas.device.details”)
。您的控制器永远不会在春季拾取。将其移至另一个软件包,或更改组件扫描。
Your controller is in package
com.gtt.wcas.device.root
but you setup@ComponentScan(basePackages = "com.gtt.wcas.device.details")
.You controller is never picked up by Spring. Either move it to another package or change your component scan.
您可以在
@componentscan
中添加com.gtt.wcas.device.root
吗?然后检查一下,我已经通过Spring Boot观察到了这个问题,有时,如果您的控制器在不同的软件包(除源软件包除外)而没有明确扫描,则不会识别端点。
另外,由于
@RestController
已经具有该定义,因此您不需要@Responsebody
在方法定义之上!Can you add
com.gtt.wcas.device.root
in@ComponentScan
? and check ,I've observed this issue with Spring boot , sometimes ,it does not identify endpoints if your Controller is in different package (other than source package) and not explicitly Scanned.
Also, you don't need
@ResponseBody
on top of method definitions since@RestController
already has that definition!