【问题标题】:Spring REST always return 404 Not FoundSpring REST 总是返回 404 Not Found
【发布时间】:2016-11-19 10:49:32
【问题描述】:

我已经使用带有 Spring 安全性的 Spring 4 设置了一个简单的 REST API

web.xml

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/mysabisabi-servlet.xml,
     classpath*:META-INF/spring/applicationContext.xml,
      classpath*:META-INF/spring/mysabisabi-security.xml
    </param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
    <servlet-name>mysabisabi-rest</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>mysabisabi-rest</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

AccountController.java

@RestController
@RequestMapping(value="/api")
public class AccountController {


    @RequestMapping(value="/update", method=RequestMethod.POST)
public ResponseEntity<GenericResponse> updateRegistrationToken(@RequestBody RegistrationToken token){
    GenericResponse response = new GenericResponse();
    //some code here

    return new ResponseEntity<GenericResponse>(response, HttpStatus.ACCEPTED);
}

}

mysabisabi-servlet.xml

<bean id="jacksonObjectMapper" class="com.mysabisabi.mapper.JsonCustomMapper" />

<bean id="jackson2HttpMessageConverter"
    class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
    <property name="objectMapper" ref="jacksonObjectMapper" />
</bean>

<mvc:annotation-driven>
    <mvc:message-converters register-defaults="true">
        <ref bean="jackson2HttpMessageConverter" />
    </mvc:message-converters>
</mvc:annotation-driven>

mysabisabi-security.xml

 <http pattern="/oauth/token" create-session="stateless" authentication-manager-ref="clientAuthenticationManager"
    xmlns="http://www.springframework.org/schema/security">
<intercept-url pattern="/oauth/token" access="isFullyAuthenticated()"/>
<csrf disabled="true"/>
<anonymous enabled="false"/>
<http-basic entry-point-ref="clientAuthenticationEntryPoint"/>
<!-- include this only if you need to authenticate clients via request parameters -->
<custom-filter ref="clientCredentialsTokenEndpointFilter" after="BASIC_AUTH_FILTER"/>
<access-denied-handler ref="oauthAccessDeniedHandler"/>
 </http>

  <http pattern="/api/*" create-session="never" entry-point-ref="oauthAuthenticationEntryPoint"
    access-decision-manager-ref="accessDecisionManager" xmlns="http://www.springframework.org/schema/security">
<anonymous enabled="false"/>
<csrf disabled="true"/>
<intercept-url pattern="/api/*"  access="hasRole('ROLE_USER')"/>
<custom-filter ref="resourceServerFilter" before="PRE_AUTH_FILTER"/>
<access-denied-handler ref="oauthAccessDeniedHandler"/>

applicationContext.xml

<mvc:annotation-driven />

<context:annotation-config />

<context:component-scan base-package="com.mysabisabi" />

<context:property-placeholder location="classpath:mysabisabi.properties" />

<tx:annotation-driven transaction-manager="transactionManager" />


//dataSource configuration

日志文件

2016-07-16 16:24:45 INFO  RequestMappingHandlerMapping:537 - Mapped "{[/api/update],methods=[POST]}" onto public org.springframework.http.ResponseEntity<com.mysabisabi.dto.response.GenericResponse> com.mysabisabi.controller.AccountController.updateRegistrationToken(com.mysabisabi.dto.request.RegistrationToken)

当我尝试访问http://localhost:8080/mysabisabi/api/update 时,我得到了HTTPStatus 404 Not Found - The requested resource is not available

我已尝试将 //* 作为 url-pattern 但仍然相同。

谁能指出我错过了什么?

谢谢。

【问题讨论】:

  • 您以哪种格式向控制器发送请求(Json/xml/纯文本)?
  • 你的 spring 配置 xml 文件在哪里。请分享 spring conf 文件。如果您使用类文件进行配置。共享类文件。
  • 您的配置看起来不错。在 web.xml 中更改以下代码。mysabisabi-restorg.springframework.web.servlet .DispatcherServletcontextConfigLocation/WEB-INF/mysabisabi-servlet.xml参数> 1。并尝试一次。同时检查应用程序日志以防出现异常/错误。
  • 是的..解决它。谢谢。您可以将其添加为答案,以便我接受。

标签: java spring rest spring-mvc


【解决方案1】:

您的配置看起来不错。 在 web.xml 中更改以下代码。

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

还要检查下面的网址。 Order of loading contextConfigLocation in web.xml of Spring Servlet project

【讨论】:

    【解决方案2】:

    你的应用容器是什么?

    例如,Tomcat 会将应用名称添加到链接中,如 http://localhost:8080/mysabisabi/api/update,而 Google App Engine 不会,如:http://localhost:8080/api/update

    【讨论】:

      猜你喜欢
      • 2012-08-16
      • 2015-06-09
      • 2020-01-12
      • 1970-01-01
      • 1970-01-01
      • 2021-09-11
      • 2017-12-27
      • 2023-01-05
      • 2016-03-16
      相关资源
      最近更新 更多