使用DispatcherServlet for RestController

时间:2017-01-16 23:36:45

标签: spring spring-mvc

我目前正在尝试了解Dispatcher Servlet如何与Rest Controller一起工作,但是Postman在我迄今尝试的所有内容上都返回了404.

其余控制器

@RestController
@RequestMapping(value = "/applications")
public class ApplicationController {


private static final Logger logger = LoggerFactory.getLogger(ApplicationController.class);

@Autowired
@Qualifier("ApplDAO")
private ApplDAO applDAO;


@Autowired
ApplicationService objServices;

@RequestMapping(value = "for_user\\{username:\\d+}", method = RequestMethod.GET)
public Application getApp(@PathVariable("username") String username){
    Application app = applDAO.getByUsername(username);
    return app;

  }
}

我的web.xml

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring4-servlet.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>springDispatcher</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>springDispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

我尝试使用url-pattern / *但没有结果。 这是我尝试访问的http://localhost:8080/project/applications/for_user/username:acid网址 我正在使用的URL是否有问题,或者我使用了调度程序错误。 这是弹簧错误

  

找不到带URI的HTTP请求的映射[/ project / applications / for_user / username:acid

1 个答案:

答案 0 :(得分:1)

JB Nizet回答

  

为什么在RequestMapping中使用反斜杠而不是斜杠?   如果你想发送用户名:acid(或者),你为什么要使用正则表达式\ d +?   酸?)作为用户名。只需使用value =“/ for_user / {username}”,然后使用   http://localhost:8080/project/applications/for_user/acid