У меня есть приложение с двумя пакетами. В первом пакете у меня 2 контроллера. Первый контроллер называется APIController.java, который отображает представление. Это код контроллера:
package com.dogo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping()
public class APIController {
@RequestMapping("/api")
public String apiChat() {
return "apiChat.html";
}
}
Второй контроллер называется HomeController.java, который я использую для отображения страницы index.html. Это код:
package com.dogo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HomeController {
@RequestMapping("/")
public String index() {
return "index.html";
}
}
Второй пакет также содержит контроллер под названием MessageController.java. Это используется для обновления базы данных dao на основе параметров, переданных в URL-адресе. Это код для этого контроллера:
package com.dogo.chat;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping()
public class MessageController {
@Autowired
MessageRepository dao;
@GetMapping("/chat")
public List<Message> getMessages(){
List<Message> foundMessages = dao.findAll();
return foundMessages;
}
@PostMapping("/chat")
public ResponseEntity<Message> postMessage(@RequestBody Message message)
{
// Saving to DB using an instance of the repo Interface.
Message createdMessage = dao.save(message);
// RespEntity crafts response to include correct status codes.
return ResponseEntity.ok(createdMessage);
}
@GetMapping("/chat/{id}")
public ResponseEntity<Message> getMessage(@PathVariable(value = "id")
Integer id){
Message foundMessage = dao.findOne(id);
if (foundMessage == null) {
return ResponseEntity.notFound().header("Message","Nothing found with that id").build();
}
return ResponseEntity.ok(foundMessage);
}
@DeleteMapping("/message/{id}")
public ResponseEntity<Message> deleteMessage(@PathVariable(value = "id") Integer id){
Message foundMessage = dao.findOne(id);
if (foundMessage == null) {
return ResponseEntity.notFound().header("Message","Nothing found with that id").build();
}else {
dao.delete(foundMessage);
}
return ResponseEntity.ok().build();
}
}
Когда я набираю http: // локальный: 8080 / api в своем браузере, отображается страница apiChat.html. Когда я меняю @RequestMapping () в APIController.java на @RequestMapping ("/ api") и набираю http: // локальный: 8080 / api / api, я получаю ошибку 404. Я ожидал, что отобразится страница apiChat.html. Что мне не хватает? Спасибо.
Я только что настроил тестовый проект, и он отлично работает, думаю, это не корень вашей проблемы
@daniu Я только что отредактировал свое сообщение, чтобы добавить больше кода из файлов, которые есть в моем приложении




Почему в вашем заголовке упоминается «несколько контроллеров»?