![](/img/trans.png)
[英]How can I directly use url to access my spring boot web application without any port?
[英]Can not access my spring boot application by url
我尝试使用本地主机 url 访问我的端点 - http://localhost:8080/all 这是我的 Application.java 文件
package com.example.MyApplication;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication .class, args);
}
}
这是我的终点
@RestController("GetAll")
@RequestMapping("/all")
public class GetAll {
private final DataService dataService;
@Autowired
public GetAll (DataService dataService) {
this.dataService = dataService;
}
@GetMapping
public List<DataDto> getAll() {
return dataService.getAll();
}
}
我尝试使用这个 url - http://localhost:8080/all
{
"timestamp": "2022-10-06T15:27:18.574+00:00",
"status": 404,
"error": "Not Found",
"path": "/all"
}
这种问题可能由于某些组件(例如controller,服务等)而发生,而不是由spring扫描,这是由于组件和主要的class在不同的package.RES com.example.MyApplication
package888888888888888888888888888881215121.因此,如果您将 spring 引导的所有其他组件(如 controller、服务等)保留在同一个 package 或子包中,那么它将按预期工作。 如果您想将它们保留在不同的包中,您需要在@ComponentScan
注释中提及那些具有 spring 组件(如控制器)的不同包。
如果可以将GetAll
类的 package 声明保留在同一个 package 中,请更改它们:
package com.example.MyApplication;
@RestController("GetAll")
@RequestMapping("/all")
public class GetAll {
private final DataService dataService;
@Autowired
public GetAll (DataService dataService) {
this.dataService = dataService;
}
@GetMapping
public List<DataDto> getAll() {
return dataService.getAll();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.