我开始制作一个测试api springboot,当我将其部署到tomcat http://localhost:8080/demoApp/api/v1/person 上时,它返回404。
这是我的控制器文件:
@RequestMapping("api/v1/person")
@RestController
public class PersonController {
private final PersonService personService;
@Autowired
public PersonController(PersonService personService) {
this.personService = personService;
}
@PostMapping
public void addPerson(@Valid @NonNull @RequestBody person person) {
personService.addPerson(person);
}
@GetMapping(path = "{id}")
public person getPersonById(@PathVariable("id") UUID id) {
return personService.getPersonById(id)
.orElse(null);
}
@GetMapping
public List<person> getAllPeople() {
return personService.getAllPeople();
}
@DeleteMapping(path = "{id}")
public void deletePersonById(@PathVariable("id") UUID id) {
personService.deletePerson(id);
}
@PutMapping(path = "{id}")
public void updatePerson(@PathVariable("id") UUID id,@Valid @NonNull @RequestBody person person) {
personService.updatePerson(id, person);
}
}`
我只是测试api,这样我就不会制作任何html文件,当我调用http://localhost:8080/demoApp/api/v1/person时,它会返回404源服务器找不到目标资源的当前表示或不愿意透露其存在。
但是当我在 http://localhost:8080/demoApp/ 添加 helloworld.html 时它仍然有效
尝试
http://localhost:8080/api/v1/person
demoApp
是您的应用程序名称,与请求路径无关。使用 @RequestMapping("api/v1/person")
和空 @GetMapping
,您的请求网址应为 http://localhost:8080
+/api/v1/person
。