【问题标题】:404 error when calling Spring controller method from Angular $http从 Angular $http 调用 Spring 控制器方法时出现 404 错误
【发布时间】:2016-05-07 21:25:39
【问题描述】:

我一直在尝试运行一个使用 Spring 4.2.5 和 AngularJs 的示例单页应用程序。我正在尝试从 Angular 的 $http 服务调用我在 Controller 中的一种方法。

在一些控制器内部:

$scope.goTosearchList = function() {
        $http.get("/rest/getSearchList").success(function(data){
            $scope.data=data;
            alert("success "+data);
        })
        $location.path("/rest/searchList");
    };

尝试在控制器中捕获此请求:

@Controller

public class MyPortController {

    @Autowired
    DatabaseDao dao;

    @RequestMapping(value="/getSearchList",method=RequestMethod.GET)
    public void searchbyid() {

        System.out.println("hello, we r in java");
        dao.databaseConnect();

    }

}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>MyPort</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>

  <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>

这是我的 spring servlet:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/mvc
                           http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="com.mmt.controller" />
    <mvc:annotation-driven></mvc:annotation-driven>
   <!--  <bean id="searchbyid" class="com.mmt.dao.DatabaseDao">
    </bean> -->

    </beans>

问题可能听起来有点幼稚,但我无法找到出路。请帮帮我。

【问题讨论】:

  • 尝试将请求路径更改为 localhost:8080/rest/getSearchList(如果您在 localhost 上运行它)。
  • 你还有其他工作处理程序吗?
  • @AlexandreFillatre 不,实际上我是第一次尝试它。我是否缺少处理程序配置?
  • @ElenaChubukina 我尝试使用它,但它会引发 404 错误。我想配置一些也可以在全球范围内工作的东西,而不仅仅是在本地。
  • 我敢打赌,您错过了包扫描配置,因此您的处理程序从未注册。你能分享你的spring-config.xml文件吗?

标签: java angularjs spring spring-mvc


【解决方案1】:

我们开始吧。

首先,你需要创建dispatcher-servlet.xml并将必要的spring配置放在这里,然后放到classpath:WEB-INF/下,所以完整路径是classpath:WEB-INF/dispatcher-servlet.xml,其中包含以下设置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema    /mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-    context.xsd">

    <context:component-scan base-package="a.b.c" />
</beans>

其中a.b.c 是控制器MyPortController 的基本包。这个配置会告诉spring自动从指定的包中扫描beans(这里是controllers)。

请注意,为什么它的名字是dispatcher-servlet.xml?这是因为 spring 将加载以&lt;servlet-name/&gt; 开头并以-servlet.xml 结尾的配置。否则,您应该在 servlet 设置中指定它,如下所示:

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/applicationContext.xml</param-value>
    </init-param>
</servlet>

它应该工作,享受它。

【讨论】:

  • 我的错误是我之前没有安装我的 dispatcher-servlet。我在使用 servlet 映射请求 url 时遇到问题。我已经在上面发布了我的所有文件,所以如果你能提供帮助,那将是非常有帮助的。
  • 请在searchbyid方法上加上@ResponseBody@RequestMapping,否则返回值将被视为重定向视图。
猜你喜欢
  • 2019-08-14
  • 1970-01-01
  • 2020-11-28
  • 2019-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-31
  • 2021-05-04
相关资源
最近更新 更多