Mr. X
Mr. X

Reputation: 3

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, annotations, or something)?

For example:

/springmvc/hello instead of /springmvc/hello_world.html.

Upvotes: 0

Views: 389

Answers (3)

K Erlandsson
K Erlandsson

Reputation: 13696

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

Upvotes: 0

earldouglas
earldouglas

Reputation: 13473

The ControllerClassNameHandlerMapping is a great way to use convention over configuration to simplify your URL bindings.

Upvotes: 0

brabster
brabster

Reputation: 43580

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.

Upvotes: 1

Related Questions