RESTful URLs with Spring 3
How do I get nice-looking URLs in Spring (without a UrlRewriteFilter)? Does Spring 3 have any convenient way of getting nice-looking URLs (like, anno开发者_Go百科tations, or something)?
For example:
/springmvc/hello instead of /springmvc/hello_world.html.
This part of your web.xml determines which requests are routed to the springmvc servlet - any ending .html in the tutorial.
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
Change the url-pattern element to you prefer more. Try <url-pattern>/*</url-pattern>
to route the requests you're mentioned without any suffix.
We use annotations in our RESTful services implemented by Spring MVC controllers. I'm not sure this is exactly what you are looking for, but our code look like this:
@Controller
@RequestMapping("/contact")
public class ContactController {
// ...
@RequestMapping(value = "/info", method = RequestMethod.GET)
public @ResponseBody
ContactInfo getContactInfo() {
return contactDelegate.getContactInfo();
}
}
The URL for accessing this controller will be http://foo.com/contact/info.
You will need to put <mvc:annotation-driven />
in your application context XML
The ControllerClassNameHandlerMapping is a great way to use convention over configuration to simplify your URL bindings.
精彩评论