1. 首页
  2. 软件开发
  3. RESTful集成到spring MVC

RESTful集成到spring MVC

  • 发布于 2024-09-20
  • 0 次阅读

1.RESTful URL和普通URL的区别

(1)URL结构的不同

  • RESTfulURL:清晰地表达资源层次,如 /products/123 表示具体ID为123的商品。

例如 https://example.com/products/123

  • 普通URL:通常使用查询参数,如 id=123 传递资源的标识

例如 https://example.com/products?id=123

(2)数据传递格式不同

  • RESTfulURL:数据通过请求体传递,通常使用JSON格式。
  • 普通URL:数据多以表单格式传递。

(3) 操作方法的表达

  • RESTfulURL:URL中不包含操作行为的动词,而是通过HTTP方法(GET、POST、PUT、DELETE)来表达。

举例 :传入的URL是 https://example.com/products/123 ,而具体要进行什么操作,要根据请求头中的方法来决定。

  • 普通URL:操作动词(如 creatProduct、getOrder)混在URL中,因为普通的URL请求头中的方法一般只用POST和GET,所以要通过你的操作动词来匹配到具体的方法。

举例:传入的URL是 https://example.com/products/creatProduct?id=123 ,这才会去调用products下的creatProduct路径下的方法,并且向该方法内传入id。

GET 和 POST 的位置 :在HTTP请求中,GET和POST等请求方法出现在 请求行的开头,它们用于指示请求类型,浏览器会自动生成这些请求并发送给服务器。

2.RESTful 与普通 URL 在后端代码上的不同

RESTful URL 示例:

@RestController
@RequestMapping("/products")
public class ProductController {

    // 获取产品列表 (GET /products)
    @GetMapping
    public List<Product> getAllProducts() {
        return productService.findAll();
    }

    // 获取特定产品 (GET /products/{id})
    @GetMapping("/{id}")
    public Product getProductById(@PathVariable Long id) {
        return productService.findById(id);
    }

    // 创建产品 (POST /products)
    @PostMapping
    public Product createProduct(@RequestBody Product product) {
        return productService.save(product);
    }

    // 更新产品 (PUT /products/{id})
    @PutMapping("/{id}")
    public Product updateProduct(@PathVariable Long id, @RequestBody Product product) {
        return productService.update(id, product);
    }

    // 删除产品 (DELETE /products/{id})
    @DeleteMapping("/{id}")
    public void deleteProduct(@PathVariable Long id) {
        productService.deleteById(id);
    }
}

(1)通过 @GetMappin、 @PostMappin、 @PutMappin、 @DeleteMappin来对应HTTP方法中的GET、POST、PUT、DELETE( 查、增、改、删),当一个请求从客户端发送到服务端,会根据请求头中的HTTP方法自动找到对应的方法,即使没有操作动词。


(2)如果/products之后没有跟任何参数,并且在请求方法为GET的情况下,就会调用@GetMapping注释下的方法。


(3)同时也可以 @GetMapping(“/{id}“),表明当/products后存在产品id的情况时会调用该注解下的方法

普通 URL 示例:

@RestController
public class ProductController {

    // 获取产品列表 (GET /getProducts)
    @RequestMapping(value = "/getProducts", method = RequestMethod.GET)
    public List<Product> getAllProducts() {
        return productService.findAll();
    }

    // 获取特定产品 (GET /getProduct)
    @RequestMapping(value = "/getProduct", method = RequestMethod.GET)
    public Product getProductById(@RequestParam Long id) {
        return productService.findById(id);
    }

    // 创建产品 (POST /createProduct)
    @RequestMapping(value = "/createProduct", method = RequestMethod.POST)
    public Product createProduct(@RequestBody Product product) {
        return productService.save(product);
    }

    // 更新产品 (POST /updateProduct)
    @RequestMapping(value = "/updateProduct", method = RequestMethod.POST)
    public Product updateProduct(@RequestParam Long id, @RequestBody Product product) {
        return productService.update(id, product);
    }

    // 删除产品 (POST /deleteProduct)
    @RequestMapping(value = "/deleteProduct", method = RequestMethod.POST)
    public void deleteProduct(@RequestParam Long id) {
        productService.deleteById(id);
    }
}

(1)这里只有一种注释@RequestMapping,对于来自客户端的请求具体要用调用哪个方法来处理则要根据@RequestMapping后的参数(value来表示请求路径,也是操作动词,method则表示要处理GET、POST、PUT、DELETE中的哪种请求)

结语

  • 如果使用 @RequestMapping ,你通常应该明确标注 method 参数,以确保该方法只处理特定的 HTTP 请求方法(如 POST 请求)。
  • 如果不标注 method 参数,Spring MVC 默认会接受所有的 HTTP 方法,这在实际开发中可能会引发问题。
  • 推荐使用简化注解 如 @PostMapping、@GetMapping 等,来减少重复的 method 参数声明,使代码更简洁且符合 RESTful 风格。


原文链接: https://blog.csdn.net/m0_73837751/article/details/142025198