首页 文章

Spring MVC REST:发出映射请求

提问于
浏览
0

我试图通过一些Spring安全配置通过Spring MVC REST公开REST API . 但是,我得到了404 .

这是代码 .

web.xml中

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app ...>
    <display-name>REST Project</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/applicationContext.xml</param-value>
    </context-param>

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

休息-的servlet:

<?xml version="1.0" encoding="UTF-8"?>
<beans ... />
    <mvc:annotation-driven />
  </beans>

MVC-配置:

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

<beans ...>

    <mvc:annotation-driven />
    <bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- Example: a logical view name of 'showMessage' is mapped to '/WEB-INF/jsp/showMessage.jsp' -->
        <property name="prefix" value="/WEB-INF/view/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

的applicationContext:

<?xml version="1.0" encoding="UTF-8"?>
<beans ... ">
    <context:component-scan base-package="org.my.project" />
</beans>

最后,服务REST控制器

@RestController
@RequestMapping("/service")
public class Service {

    @RequestMapping(value = "/public/{name}", method = RequestMethod.GET)
    public String storeEntityPublic(@PathVariable String name) {
        // do something

        return result;
    }
}

URL:localhost:8080 // service / public / aname

我错过了什么?

1 回答

  • -1

    在您的 localhost:8080/service/public/aname 中,缺少上下文名称,这就是您收到404错误的原因 . URL应该像 localhost:8080/abc/service/public/aname ,这里abc是我的项目或上下文名称 . 所以请添加上下文名称并再次点击新网址 .

相关问题