【问题标题】:No mapping found for HTTP request with URI in Restful在 Restful 中找不到带有 URI 的 HTTP 请求的映射
【发布时间】:2016-10-20 19:30:13
【问题描述】:

在我使用restful json的项目中,我有一些文件如下:

web.xml:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Processes application requests -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

Servlet-context.xml

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

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<resources mapping="/images/**" location="/images/" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean>   
<context:component-scan base-package="com.totoroads.webservice.android" />
</beans:beans>

我的泽西课:

@Path("/marker")
public class GetLatLongMapMarker {

@GET 
@Path("/doMarker")
@Produces("application/json;charset=utf-8")
public String doMap() {

    String carMaps = null;

    ArrayList<CarMap> mapList = new ArrayList<CarMap>();

        try {
            mapList = DBConnection.getAllMapMarker();
            Gson gson = new Gson();
            carMaps = gson.toJson(mapList); 
        } 

        catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   

    return carMaps;
    }
}

当我想通过 URL 从 json 中获取数据时:

http://192.168.1.221:9999/restfulspringmvc/maker/doMarker

发生了异常,这是日志:

02:12:23.239 [http-bio-9999-exec-3] DEBUG o.s.web.servlet.DispatcherServlet   - DispatcherServlet with name 'appServlet' processing GET request for   [/restfulspringmvc/maker/doMarker]
02:12:23.239 [http-bio-9999-exec-3] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Looking up handler method for path /maker/doMarker
02:12:23.240 [http-bio-9999-exec-3] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Did not find handler method for [/maker/doMarker]
02:12:23.241 [http-bio-9999-exec-3] WARN  o.s.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/restfulspringmvc/maker/doMarker] in DispatcherServlet with name 'appServlet'
02:12:23.245 [http-bio-9999-exec-3] DEBUG o.s.web.servlet.DispatcherServlet - Successfully completed request

如何修复此配置? 非常感谢!!

【问题讨论】:

    标签: java json spring rest


    【解决方案1】:

    您正在以这种方式在球衣类中定义请求映射:

    @Path("/marker")
    public class GetLatLongMapMarker {
    
    @GET 
    @Path("/doMarker")
    @Produces("application/json;charset=utf-8")
    public String doMap() {
    

    这会导致 /[context-path]/marker/doMarker,但您正在请求此 url http://192.168.1.221:9999/restfulspringmvc/maker/doMarker

    所以将您的请求中的maker 更改为marker,您应该可以访问它

    【讨论】:

    • 我已从制造商更改为标记器,但错误相同。它似乎发生在 servlet-context.xml 文件中的问题
    猜你喜欢
    • 1970-01-01
    • 2016-02-05
    • 2012-04-16
    • 1970-01-01
    • 2017-09-17
    • 2016-06-11
    • 2017-03-02
    • 1970-01-01
    相关资源
    最近更新 更多