Spring 4 Rest服务404错误

时间:2016-05-27 06:42:47

标签: java spring rest spring-mvc

我正在学习spring 4 mvc并创建一个简单的Web应用程序。我有一个带有REST端点的Spring 4后端可以从我的AngularJS前端命中。问题是每当我从前端点击这些端点时,请求返回404。

这是我的Spring配置文件

的web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>


  <!-- Spring MVC -->
  <servlet>
      <servlet-name>mvc-dispatcher</servlet-name>

      <servlet-class>
          org.springframework.web.servlet.DispatcherServlet
      </servlet-class>

      <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
      <servlet-name>mvc-dispatcher</servlet-name>
      <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

MVC-调度-servlet.xml中

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

  <bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
      <value>/WEB-INF/pages/</value>
    </property>
    <property name="suffix">
      <value>.jsp</value>
    </property>
  </bean>

</beans>

loginController.java

package controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;



@RestController
public class loginController {



    @RequestMapping(value = "/user", method = RequestMethod.GET)
    public String listAllUsers() {
        return "test";

    }
}

我尝试过很多不同的方式来改变我的配置,但似乎没有任何工作。什么可能导致这里的问题?

1 个答案:

答案 0 :(得分:1)

正如@Ali在评论中写的那样,你应该有

DB::table('pharmacies')
     ->select('pharmacies.*','addresses.*',
               DB::raw('(6371 * acos( cos( radians(32.5033801) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(74.49884399999999) ) + sin( radians(32.5033801) ) * sin( radians( lat ) ) ) ) AS distance'))

     ->join('addresses','pharmacies.address_id','=','addresses.address_id')
     ->having('distance','<',25)
     ->orderBy('distance','asc');

<mvc:annotation-driven/> <context:component-scan base-package="controller" />

有关详细信息,请参阅http://docs.spring.io/autorepo/docs/spring/4.2.x/spring-framework-reference/html/mvc.html