当前位置:网站首页>RESTful风格
RESTful风格
2022-06-28 09:15:00 【多喝清晨的粥】
一、导入依赖
<!--java Web工程要记得打成war包-->
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<!--JSON 数据转换支持-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>
二、实体类
public class User {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
三、配置文件
springMVC.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--开启注解扫描-->
<context:component-scan base-package="com.ceshi.controller"/>
<!--处理器适配器 处理器映射器-->
<mvc:annotation-driven/>
<!-- spring官方团队 很看重 静态资源的处理如果我们拦截路径采用/的形式 需要对静态资源放行 -->
<mvc:default-servlet-handler/>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!--编码过滤器-->
<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:springMVC.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<!--<url-pattern>*.do</url-pattern>-->
<url-pattern>/</url-pattern>
</servlet-mapping>
四、controller层
/** * RESTful风格 */
@Controller
@RequestMapping("/user")
public class UserController {
@GetMapping("/{id}")
public @ResponseBody String testGet(@PathVariable("id") Integer id){
System.out.println("get:"+id);
return "查询成功";
}
@PostMapping
public @ResponseBody String testPost(@RequestBody User user){
System.out.println("post:"+user);
return "新增成功";
}
@PutMapping
public @ResponseBody String testPut(@RequestBody User user){
System.out.println("put:"+user);
return "更新成功";
}
@DeleteMapping("/{id}")
public @ResponseBody String testDelete(@PathVariable("id") Integer id){
System.out.println("delete:"+id);
return "删除成功";
}
}
五、web资源
略…抱歉哈…感兴趣测试的留言我哈
边栏推荐
- Which securities company is better and safer to choose when opening an account for the inter-bank certificate of deposit fund with mobile phone
- Full link service tracking implementation scheme
- Copy & Deepcopy
- Characteristics and prevention of electrical fire
- 在本类私有属性直接使用?new()在使用!!!
- 1181: integer parity sort
- Campus honey decoration of APP course design (e-commerce platform)
- Implementation of code scanning login
- Common tools for interface testing --postman
- 理解IO模型
猜你喜欢

学习阿里如何进行数据指标体系的治理

Valentine's Day - VBS learning (sentences, love words)

Using transform:scale causes the page mouse hover event to disappear

How to solve the problem of high concurrency and seckill

DEJA_ Vu3d - 051 of cesium function set - perfect realization of terrain excavation

Write a simple timeline

Interpretation of new products: realm launched GT neo2 Dragon Ball customized version
![1180:分数线划定/P1068 [NOIP2009 普及组] 分数线划定](/img/1a/162b060a6498e58278b6ca50e4953c.png)
1180:分数线划定/P1068 [NOIP2009 普及组] 分数线划定

new URL(“www.jjj.com“)

This article explains in detail the difficult problems and solutions faced by 3D cameras
随机推荐
This article explains in detail the difficult problems and solutions faced by 3D cameras
Potential safety hazards in elderly care facilities
Fire safety hazards
Rman Backup Report Ora - 19809 Ora - 19804
Copy & Deepcopy
JMeter -- interface test 1
Application of current limiting protector in preventing electrical fire in shopping malls
1181: integer parity sort
104. maximum depth of binary tree
Divide and rule classic Hanoi
rman備份報ORA-19809 ORA-19804
Fire fighting work and measures in Higher Vocational Colleges
JMeter -- interface test 2
Fastjason filter field
Campus honey decoration of APP course design (e-commerce platform)
Data mining modeling practice
The constructor is never executed immediately after new()!!!!!
Common test method used by testers --- orthogonal method
Calculation of stock purchase and sale expenses
01-分布式系统概述