Java Spring Tool Suite多个控制器

时间:2018-04-04 14:14:57

标签: java spring

我有两个包的应用程序。在第一个包中,我有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的控制器。这用于根据url中传递的参数更新dao数据库。这是该控制器的代码:

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://localhost:8080/api时,会显示apiChat.html页面。当我将APIController.java中的@RequestMapping()更改为@RequestMapping(&#34; / api&#34;)并输入http://localhost:8080/api/api时,我收到404错误。我原本期望显示apiChat.html页面。我错过了什么?谢谢。

0 个答案:

没有答案