当前位置:网站首页>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
边栏推荐
- 线程的生命周期
- PMP Exam key summary VI - chart arrangement
- Expérience d'optimisation SQL: de 30248 secondes à 0001 secondes
- Machine virtuelle 14 installer win7 (tutoriel)
- new URL(“www.jjj.com“)
- flink cep 跳过策略 AfterMatchSkipStrategy.skipPastLastEvent() 匹配过的不再匹配 碧坑指南
- Write a simple timeline
- A classic JVM class loaded interview question class singleton{static singleton instance = new singleton(); private singleton() {}
- DBeaver安装与使用教程(超详细安装与使用教程)
- Custom exception classes and exercises
猜你喜欢

Key summary V of PMP examination - execution process group
![1180: fractional line delimitation /p1068 [noip2009 popularization group] fractional line delimitation](/img/1a/162b060a6498e58278b6ca50e4953c.png)
1180: fractional line delimitation /p1068 [noip2009 popularization group] fractional line delimitation

Expérience d'optimisation SQL: de 30248 secondes à 0001 secondes

HDI的盲孔设计,你注意到这个细节了吗?

Interpretation of new products: realm launched GT neo2 Dragon Ball customized version

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

new URL(“www.jjj.com“)

布隆过滤器 课程研究报告

English translation plug-in installation of idea

Prototype chain JS
随机推荐
What is online account opening? Is it safe to open an account online now?
Use of Jasper soft studio report tool and solution of thorny problems
PMP needs to master its own learning methods
Implementation of single sign on
1182: effets de la photo de groupe
剑指Offer | 链表转置
SQL 優化經曆:從 30248秒到 0.001秒的經曆
new URL(“www.jjj.com“)
new URL(“www.jjj.com“)
HDI blind hole design, have you noticed this detail?
数字人行业爆发在即,市场格局几何?
Key summary V of PMP examination - execution process group
Regular verification of mobile phone number and email [easy to understand]
SQL 优化经历:从 30248秒到 0.001秒的经历
PMP考试重点总结七——监控过程组(1)
Android studio interview preparation
Threads and processes
JDBC connection database (MySQL) steps
什么是在线开户?现在网上开户安全么?
The concept of "tree structure" perfectly interprets the primary and secondary of things