【问题标题】:Rest web sercive path Not Found未找到休息 Web 服务路径
【发布时间】:2016-08-26 02:00:04
【问题描述】:

尝试通过休息客户端http://localhost:8080/gurukul/userList 访问我的网络服务,但它显示错误“未找到”。

{
"timestamp": 1462078050576
"status": 404
"error": "Not Found"
"message": "No message available"
"path": "/gurukul/userList"
}

下面是我的控制器,我在其中通过 RequestMapping 添加了路径,但在 spring-boot 之后仍然找不到路径

package controller;

    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.bind.annotation.RestController;

    import bean.UserList;
    import common.Status;
    import constants.Constants;
    import constants.Messages;
    import dao.UserListDAO;

    @EnableAutoConfiguration
    @Controller
    @RestController
    @RequestMapping("gurukul")
    public class GurukulController {

        @RequestMapping(value = "/userList", method = RequestMethod.GET)
        @ResponseBody
        public Map<String, Object> getUsersList() {
            ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Module.xml");

            UserListDAO userListDAO = (UserListDAO) context.getBean("userListDAO");
            Map<String, Object> resultMap = new HashMap<>();
            List<UserList> listResult = userListDAO.getUsers();
            if (listResult != null) {
                resultMap.put(Constants.RESULT, listResult);
                resultMap.put(Constants.STATUS, Status.SUCCESS);
            } else {
                resultMap.put(Constants.RESULT, Messages.UNABLE_TO_GET_USERS_LIST);
                resultMap.put(Constants.STATUS, Status.FAILURE);
            }
            return resultMap;
        }

    }

    enter code here

【问题讨论】:

  • 您是使用嵌入式容器还是部署到独立容器?如果是前者,你的主类是什么样的,它在什么包里?如果是后者,你扩展SpringBootServletInitializer了吗?无论哪种情况,您都使用什么上下文路径? minimal, complete, verifiable example 在这里会非常有用

标签: java spring web-services rest spring-boot


【解决方案1】:

您的 URL 应该是 http://localhost:8080/context root/Controller 类路径/服务方法路径

使用http://localhost:8080/context根路径/gurukul/userList

【讨论】:

  • 这与路径无关,因为如果我将控制器放在 MainAppliction 程序所在的包中,那么在运行 spring-boot 之后,我可以访问我的 Web 服务。但是如果我将 MyController 放入控制器包中,则找不到路径
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-16
  • 2014-07-27
  • 2016-07-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多