@RequestMapping(value = "/api/products", method = RequestMethod.GET) public String getProductsByCategory(@RequestParam("category") String category) { } @RequestMapping(value = "/api/products/{category}", method = RequestMethod.GET) public String getProductsByCategory(@PathVariable("category") String category) { } @RequestMapping(value = "/api/products", method = RequestMethod.POST) public String addProduct(@ModelAttribute Product product) { } <!-- web.xml --> <web-app> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/config/springmvc-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app> <!-- springmvc-config.xml --> <beans> <context:component-scan base-package="com.example.controller" /> <mvc:annotation-driven /> </beans> @Controller public class ProductController { @RequestMapping(value = "/api/products", method = RequestMethod.GET) public String getProductsByCategory(@RequestParam("category") String category) { } @RequestMapping(value = "/api/products/{category}", method = RequestMethod.GET) public String getProductsByCategoryPath(@PathVariable("category") String category) { } @RequestMapping(value = "/api/products", method = RequestMethod.POST) public String addProduct(@ModelAttribute Product product) { } }


上一篇:
下一篇:
切换中文