How are absolute paths applied when combining @RequestMapping, @RestController and web.xml, and why does my test fail?

We have a servlet defined in our web.xml:

<servlet>
    <servlet-name>foo</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>foo</servlet-name>
    <url-pattern>/foo/*</url-pattern>
</servlet-mapping>

In our controller class, we use both @RequestMapping, and @RestController.

package com.example.foo;

@RestController("/foo/bar/v1")
public class Baz {
  @RequestMapping(value="/bar/v1/abc" /* ... */)
  public String doXyz() { 

Now I know Spring documentation for RequestMapping says

When used at the type level, all mappings at the method level inherit this primary mapping, narrowing it down for a particular handler method.

, @RequestMapping("/foo") , , , . , @RequestMapping("/foo/bar/v1") , @RestController, /foo/bar/v1/bar/v1/abc - /foo/bar/v1/abc, . ?

, -, @RestController("/foo/bar/v1") , @RequestMapping("/foo/bar/v1") - - ?

, ? /foo/ web.xml?

, , :

    MvcResult result = mockMvc.perform(
            get("/foo/bar/v1")
            .accept(MediaType.APPLICATION_JSON_VALUE) //...

, ,

  • web.xml, /foo
  • @RequestMapping("bar/v1") @RestController("/foo/bar/v1").

, ,

javax.servlet.ServletException: [com.example.foo.Baz@5b800468]: DispatcherServlet HandlerAdapter,

, ( @WebAppConfiguration):

<context:annotation-config/>
<mvc:annotation-driven/>

<context:component-scan base-package="com.example.foo" />

web.xml ?

+4
1

, @RequestMapping ( "/foo" ) , , , . , @RequestMapping ( "/foo/bar/v1" ) , @RestController, /foo/bar/v 1/bar/v1/abc - /foo/bar/v 1/abc, . , ?

@RequestMapping(value="/foo/bar/v1") @RequestMapping(value="/bar/v1/abc") , url /foo/bar/v1/bar/v1/abc, (.. /foo/bar/v1/abc)

@RestController ( "/foo/bar/v1" ) , @RequestMapping ( "/foo/bar/v1" ) - ?

, , @RestController("/foo/bar/v1") , URL- , bean /foo/bar/v1, URL- .

web.xml , , ?

servlet-mapping web.xml url DispatcherServlet (Front Controller), DispatcherServlet Controller, URL- RequestMapping.

url /foo/bar/v1, , :

@RestController
@RequestMapping(value="/foo/")
public class Baz {

  @RequestMapping(value="bar/v1", method=RequestMethod.GET)
  public String doXyz() { 

  }
}
+1

Source: https://habr.com/ru/post/1673109/


All Articles