
1、在web.xml配置spring
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
- version="3.0">
-
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath*:applicationContext.xml</param-value>
- </context-param>
- <!--启动服务器时,通过监听器加载spring运行环境-->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <!--用于解决中文乱码-->
- <filter>
- <filter-name>CharacterEncodingFilter</filter-name>
- <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
- <init-param>
- <param-name>encoding</param-name>
- <param-value>UTF-8</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>CharacterEncodingFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <!--拦截-->
- <servlet>
- <servlet-name>DispatcherServlet</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath*:spring-mvc.xml</param-value>
- </init-param>
- </servlet>
- <servlet-mapping>
- <servlet-name>DispatcherServlet</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
- </web-app>
2、
- @RestController
- @RequestMapping("/user")
- public class UserController {
-
- @Autowired
- private UserService userService;
-
- @PostMapping
- public boolean save(User user) {
- return userService.save(user);
-
- }
-
- @PutMapping
- public boolean update(User user) {
- return userService.update(user);
- }
-
- @DeleteMapping("/{uuid}")
- public boolean delete(@PathVariable int uuid) {
- return userService.delete(uuid);
- }
-
- @GetMapping("/{uuid}")
- public User get(@PathVariable int uuid) {
- return userService.get(uuid);
- }
-
- @GetMapping("/{page}/{size}")
- public PageInfo<User> getAll(@PathVariable int page, @PathVariable int size) {
- return userService.getAll(page, size);
- }
-
- @PostMapping("/login")//为了和save的请求做区分
- public User login(String userName, String password) {
- return userService.login(userName, password);
- }
- }
评价
排名
4
文章
473
粉丝
3
评论
2
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:
50010702506256


欢迎加群交流技术