REST风格,页面和后台数据交互
-
REST ,表现形式状态转换,资源描述风格。
-
隐层资源的访问行为,无法通过地址得知对资源是何种操作
-
书写简化。
-
动作行为区分:
-
GET(查询)select
-
POST(新增/保存)insert
-
PUT(修改/更新)update
-
DELETE(删除)delete
-
-
访问路经模块名称通常使用复数,也就是加s的格式描述,表示此类资源,而非单个资源。
例:http://localhost/users
-
根据REST风格对资源进行访问称为RESTful
在参数旁边写
-
@PathVariable:用于接收路径参数,使用{参数名称}描述路径参数。
-
@RequestBody:用于接收josn数据
-
@RequestParam;用于接收url地址传参或者表单。绑定参数
在方法上
-
@ResponseBody:用于响应json
config
1.SpringMvcConfig.java
package com.yang.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
/**
* 这个是SpringMvc的配置类
*/
@Configuration
@ComponentScan({"com.yang.controller","com.yang.config"})
@EnableWebMvc
public class SpringMvcConfig {
}
2.
package com.yang.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
/***
* 这个是对访问mvc路径的拦截,配置放行
*/
@Configuration
public class SpringMvcSupport extends WebMvcConfigurationSupport {
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
//当访问/pages/???路径的时候,走pages目录下的内容
registry.addResourceHandler("/pages/**").addResourceLocations("/pages/");
registry.addResourceHandler("/js/**").addResourceLocations("/js/");
registry.addResourceHandler("/css/**").addResourceLocations("/css/");
}
}container
SpringMvcInitContainer.java
package com.yang.container;
import com.yang.config.SpringMvcConfig;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
import javax.servlet.Filter;
/**
* 这个类是springMvc的容器类,进行初始化
* 1.可以加载Spring配置
* 2.可以加载SpringMvc的配置
* 3.可以配置SpringMvc的的拦截路径,
* 4.可以配置Spring过滤器,对于字符集的配置。
*/
public class SpringMvcInitContainer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[0];
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{SpringMvcConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
//ctrl+O 重写的快捷键
@Override
protected Filter[] getServletFilters() {
CharacterEncodingFilter encodingFilter = new CharacterEncodingFilter();
encodingFilter.setEncoding("UTF-8");
return new Filter[]{encodingFilter};
}
}controller
1.bookController.java
package com.yang.controller;
import com.yang.domain.Book;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/books")
public class BookController {
@PostMapping
public String save(@RequestBody Book book){
System.out.println("dream..."+book);
return "{'model':'bookSave...'}";
}
@GetMapping
public List<Book> selectAll(){
List<Book> bookList = new ArrayList<>();
Book book1 = new Book();
book1.setId(1);
book1.setType("计算机类型");
book1.setBookName("java");
book1.setDescription("是一本不错的书");
Book book2 = new Book();
book2.setId(2);
book2.setType("机器人类型");
book2.setBookName("python");
book2.setDescription("一个机器人类型");
bookList.add(book1);
bookList.add(book2);
return bookList;
}
}2.userController.java
package com.yang.controller;
import com.yang.domain.User;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/users")
public class UserController {
@GetMapping("/{id}")
public String findById(@PathVariable Integer id){
System.out.println("GET..."+id);
return "{'dream' :'hello' }";
}
@PostMapping
public String add(@RequestBody User user){
System.out.println("POST..."+user);
return "{'dream' :'hello' }";
}
@PutMapping
public String update(@RequestBody User user){
System.out.println("PUT..."+user);
return "{'dream' :'hello' }";
}
@DeleteMapping("/{id}")
public String deleteById(@PathVariable Integer id){
System.out.println("DELETE..."+id);
return "{'dream' :'hello' }";
}
}pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>restful</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
//要配置成这个范围,否则会和tomcat的jar包冲突
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<port>8080</port>
<path>/</path>
<uriEncoding>UTF-8</uriEncoding>//过滤器配置后,还乱码,在这边加上
</configuration>
</plugin>
</plugins>
</build>
</project>
原创文章,作者:1402239773,如若转载,请注明出处:https://blog.ytso.com/275728.html