当前位置:网站首页>Restful style
Restful style
2022-06-28 09:36:00 【Eat more porridge in the morning】
One 、 Import dependence
<!--java Web Remember to complete the project war package -->
<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 Data conversion support -->
<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>
Two 、 Entity class
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 + '\'' +
'}';
}
}
3、 ... and 、 The configuration file
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">
<!-- Turn on annotation scanning -->
<context:component-scan base-package="com.ceshi.controller"/>
<!-- Processor adapter Processor mapper -->
<mvc:annotation-driven/>
<!-- spring Official team It's very important to The processing of static resources if we use the interception path / In the form of Need to release static resources -->
<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">
<!-- Coding filter -->
<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> <!-- Front controller -->
<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>
Four 、controller layer
/** * RESTful style */
@Controller
@RequestMapping("/user")
public class UserController {
@GetMapping("/{id}")
public @ResponseBody String testGet(@PathVariable("id") Integer id){
System.out.println("get:"+id);
return " The query is successful ";
}
@PostMapping
public @ResponseBody String testPost(@RequestBody User user){
System.out.println("post:"+user);
return " Added successfully ";
}
@PutMapping
public @ResponseBody String testPut(@RequestBody User user){
System.out.println("put:"+user);
return " The update is successful ";
}
@DeleteMapping("/{id}")
public @ResponseBody String testDelete(@PathVariable("id") Integer id){
System.out.println("delete:"+id);
return " Delete successful ";
}
}
5、 ... and 、web resources
A little … Sorry ha … I am interested in the test
边栏推荐
- 1182: effets de la photo de groupe
- Flip CEP skip policy aftermatchskipstrategy Skippastlastevent() matched no longer matches the Bikeng Guide
- 适配器模式(Adapter)
- File operations in QT
- 1181: integer parity sort
- PMP needs to master its own learning methods
- Screen settings in the source code of OBS Live Room
- Valentine's Day - VBS learning (sentences, love words)
- SQL optimization experience: from 30248 seconds to 0.001 seconds
- 自动转换之-面试题
猜你喜欢

Ingersoll Rand panel maintenance IR Ingersoll Rand microcomputer controller maintenance xe-145m

How to reduce the risk of project communication?

Data visualization makes correlation analysis easier to use

桥接模式(Bridge)

适配器模式(Adapter)

Expérience d'optimisation SQL: de 30248 secondes à 0001 secondes
![[ybtoj advanced training guide] maximum separation [hash] [Floyd]](/img/86/542ab1728a2ddbc01592b2fa83491a.jpg)
[ybtoj advanced training guide] maximum separation [hash] [Floyd]

Stock suspension

PMP考试重点总结六——图表整理

new URL(“www.jjj.com“)
随机推荐
For the development of short video app, the elder warned me to choose the open source code
Automatic conversion - interview questions
Campus honey decoration of APP course design (e-commerce platform)
满电出发加速品牌焕新,长安电动电气化产品吹响“集结号”
Wechat applet development log
Boundary value analysis method for learning basic content of software testing (2)
==和eqauls()的区别
Data mining modeling practice
Calculation of stock purchase and sale expenses
Calcul des frais d'achat et de vente d'actions
Ingersoll Rand面板维修IR英格索兰微电脑控制器维修XE-145M
PMP Exam key summary IX - closing
[ybtoj advanced training guidance] class roll call [string hash]
构造方法绝不是在new()之后就立马执行!!!!!
買賣股票費用計算
PMP考试重点总结八——监控过程组(2)
Data modeling based on wide table
适配器模式(Adapter)
1182: group photo effect
Stock suspension