Java:SpringMvc--REST风格

一、REST风格操作

1、pojo

package com.islunatic.pojo;

public class Book {
    private int id;
    private String type;
    private String name;
    private String description;
    public Book() {
    }
    public Book(int id, String name, String type, String description) {
        this.id = id;
        this.name = name;
        this.type = type;
        this.description = description;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public String toString() {
        return "Book{id = " + id + ", name = " + name + ", type = " + type + ", description = " + description + "}";
    }
}

2、配置类

package com.islunatic.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@ComponentScan({"com.islunatic.controller","com.islunatic.config"})
@EnableWebMvc//开启自动转化为json数据的支持
public class SpringMvcConfig {
}
package com.islunatic.config;

import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
import jakarta.servlet.Filter;

public class SpringMvcInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
    @Override
    protected Class[] getRootConfigClasses() {
        return new Class[]{};
    }
    @Override
    protected Class[] getServletConfigClasses() {
        return new Class[]{SpringMvcConfig.class};
    }
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
    //乱码处理
    @Override
    protected Filter[] getServletFilters() {
        CharacterEncodingFilter filter = new CharacterEncodingFilter();
        filter.setEncoding("UTF-8");
        return new Filter[]{filter};
    }
}

3、Controller类

package com.islunatic.controller;

import com.islunatic.pojo.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(book);
        return "success";
    }
    @GetMapping
    public List getAll(){
        List bookList = new ArrayList<>();
        Book book1 = new Book();
        book1.setType("计算机");
        book1.setName("SpringMVC1");
        book1.setDescription("开启");
        bookList.add(book1);
        Book book2 = new Book();
        book2.setType("计算机");
        book2.setName("SpringMVC2");
        book2.setDescription("进阶");
        bookList.add(book2);
        return bookList;
    }
}

4、解析

当有形参的时候,需要添加@PathVariable来接收路径参数

名称 @PathVariable
类型 形参注解
位置 SpringMVC控制器方法形参定义前面
作用 绑定路径参数与处理器方法形参间的关系,要求路径参数名与形参名一一对应

@RequestParam用于接收url地址传参或表单传参;@RequestBody用于接收json数据;

@PathVariable用于接收路径参数,使用{参数名称}描述路径参数。

名称 @RestController
类型 ==类注解==
位置 基于SpringMVC的RESTful开发控制器类定义上方
作用 设置当前控制器类为RESTful风格, 等同于@Controller与@ResponseBody两个注解组合功能
名称 @GetMapping @PostMapping @PutMapping @DeleteMapping
类型 ==方法注解==
位置 基于SpringMVC的RESTful开发控制器方法定义上方
作用 设置当前控制器方法请求访问路径与请求动作,每种对应一个请求动作, 例如@GetMapping对应GET请求
相关属性 value(默认):请求访问路径

你可能感兴趣的:(Spring,SpringMVC,Mybatis,restful,json,后端)