当前位置:网站首页>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考试重点总结五——执行过程组
- Resource scheduling and task scheduling of spark
- Key summary V of PMP examination - execution process group
- Summary of PMP learning experience
- ==和eqauls()的区别
- Virtual machine 14 installing win7 (Figure tutorial)
- Sword finger offer | linked list transpose
- PMP考试重点总结四——规划过程组(2)
- Regular verification of mobile phone number and email [easy to understand]
- 为什么SELECT * 会导致查询效率低?
猜你喜欢

Full link service tracking implementation scheme

File operations in QT

Data visualization makes correlation analysis easier to use

Write a simple timeline

Stock suspension
Understanding the IO model

Resource scheduling and task scheduling of spark

Automatic conversion - interview questions

Apache Doris becomes the top project of Apache

Prototype chain JS
随机推荐
[share OpenGL tutorial]
Key summary V of PMP examination - execution process group
PMP考试重点总结七——监控过程组(1)
线程和进程
详解final、finally和finalize
Divide and rule classic Hanoi
组合模式(Composite Pattern)
Write a simple timeline
P2394 yyy loves Chemistry I
==和eqauls()的区别
spark的资源调度和任务调度
Static page of pinyougou mall
Data modeling based on wide table
构造方法绝不是在new()之后就立马执行!!!!!
Check whether the table contains rows SQL Server 2005 - check whether a table contains rows or not SQL Server 2005
Valentine's Day - VBS learning (sentences, love words)
Thread lifecycle
结巴分词器_分词器原理
For the development of short video app, the elder warned me to choose the open source code
1182: group photo effect