java - Spring MVC mapping with path variable -
i have problem in big application rich context, , spent time understand why isn't mapped. tried create simple project , face same issue :(
pom.xml
<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelversion>4.0.0</modelversion> <groupid>com.springapp</groupid> <artifactid>testmvc</artifactid> <packaging>war</packaging> <version>1.0-snapshot</version> <name>testmvc</name> <properties> <spring.version>4.1.1.release</spring.version> </properties> <dependencies> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-core</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-web</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupid>javax.servlet</groupid> <artifactid>servlet-api</artifactid> <version>2.5</version> </dependency> <dependency> <groupid>javax.servlet.jsp</groupid> <artifactid>jsp-api</artifactid> <version>2.1</version> <scope>provided</scope> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-webmvc</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-test</artifactid> <version>${spring.version}</version> <scope>test</scope> </dependency> <dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupid>com.fasterxml.jackson.core</groupid> <artifactid>jackson-core</artifactid> <version>2.4.3</version> </dependency> <dependency> <groupid>com.fasterxml.jackson.core</groupid> <artifactid>jackson-databind</artifactid> <version>2.4.3</version> </dependency> </dependencies> <build> <finalname>testmvc</finalname> <plugins> <plugin> <artifactid>maven-compiler-plugin</artifactid> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> <plugin> <artifactid>maven-surefire-plugin</artifactid> <configuration> <includes> <include>**/*tests.java</include> </includes> </configuration> </plugin> <plugin> <groupid>org.mortbay.jetty</groupid> <artifactid>maven-jetty-plugin</artifactid> <version>6.1.10</version> <configuration> <scanintervalseconds>10</scanintervalseconds> <connectors> <connector implementation="org.mortbay.jetty.nio.selectchannelconnector"> <port>8080</port> <maxidletime>60000</maxidletime> </connector> </connectors> </configuration> </plugin> </plugins> </build> </project>
web.xml
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>spring mvc application</display-name> <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>
context:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" 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"> <mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.json.mappingjackson2httpmessageconverter"/> </mvc:message-converters> </mvc:annotation-driven> <context:component-scan base-package="com.springapp.mvc"/> <bean class="org.springframework.web.servlet.view.internalresourceviewresolver"> <property name="prefix" value="/web-inf/pages/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
controller:
package com.springapp.mvc; import org.springframework.web.bind.annotation.pathvariable; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.restcontroller; import java.util.hashmap; import java.util.map; import static org.springframework.web.bind.annotation.requestmethod.get; @restcontroller @requestmapping(value = "/items", produces = "application/json") public class hellocontroller { @requestmapping(method = get, value = "/${id}/item") public map<string, object> printwelcome(@pathvariable("id") string id) { map<string, object> model = new hashmap<>(1); model.put("item", id); return model; } @requestmapping(method = get, value = "/clear") public map<string, object> printwelcome() { map<string, object> model = new hashmap<>(); model.put("item", null); return model; } }
tests:
get http://localhost:8080/testmvc/items/clear => {"item":null} http://localhost:8080/testmvc/items/1/item => 404 not found
what wrong?
replace value = "/${id}/item")
value ="/{id}/item"
. not el expression , no $ needed here.
if want regrex expression path variable, put {}
. syntax {varname:regex}
first part defines variable name , second - regular expression .
check spring mvc reference more details of uri template patterns.
Comments
Post a Comment