【问题标题】:Spring rest vs standard Rest?弹簧休息与标准休息?
【发布时间】:2018-02-01 03:19:58
【问题描述】:

在 spring doc 中,我可以得到以下关于 spring mvc 和 spring rest 区别的解释。 Spring REST 架构也是基于 Spring MVC,在 View 部分略有不同。传统的 Spring MVC 依赖 View 技术来渲染模型数据,Spring REST 架构也是如此,只是模型对象直接设置为 HTTP 响应,@ResponseBody 自动将其转换为 JSON/XML。 RESTful Web 服务的输出必须是 JSON 或 XML,这是一种可以在不同的消费者应用程序平台上轻松处理的标准格式。

但是在https://en.wikipedia.org/wiki/Representational_state_transfer。 除了 json 响应之外,它还有一些功能,其余的将使用 HTTP PUT/DELETE/POST 方法来操作资源。

我想知道是否可以将以下弹簧控制器视为一项宁静的服务。我使用@RestController 来返回json 响应,但没有使用任何其他的rest 功能。

@RestController
@RequestMapping(value = "/employee")
public class EmployeeController {

    @RequestMapping(value = RequestAction.LOADLIST, method = RequestMethod.POST)
    public List<Employee>  list(@RequestBody Employee bo) {
        System.out.println(bo);
        return employeeList;

    }


    @RequestMapping(value = RequestAction.LOAD, method = RequestMethod.POST)
    public Employee getEmployee(
            @RequestBody Employee input) {
        for (Employee employee : employeeList) {
            if (employee.getId().equals(input.getId())) {
                return employee;
            }
        }
        return input;
    }


    @RequestMapping(value = RequestAction.ADD, method = RequestMethod.POST)
    public Employee addEmployee(@RequestBody Employee bo) {
        System.out.println(bo);
        return bo;
    }

    @RequestMapping(value = RequestAction.UPDATE, method = RequestMethod.POST)
    public Employee updateEmployee(@RequestBody Employee bo) {
        System.out.println(bo);
        for (Employee employee : employeeList) {
            if (employee.getId().equals(bo.getId())) {
                employee.setName(bo.getName());
                return employee;
            }
        }
        return bo;
    }
}

【问题讨论】:

  • 您似乎将 设计模式 (REST) 与可用于实现 REST 或许多其他工具的 工具 (Spring) 混淆了模式。你当然可以在 Spring 中实现 REST,但我注意到你的例子不是一个好例子——你使用一堆自定义 URI 来描述动作而不是使用 HTTP 动词(例如,updateEmployee 应该是 @ 987654325@).
  • 你可以使用 Spring 实现 RESTFUL API,为了让你的控制器更加安静,你可以遵循 REST 模型,你可以参考martinfowler.com/articles/richardsonMaturityModel.html

标签: java spring rest spring-mvc restful-architecture


【解决方案1】:

您的示例脚本不是 REST,因为它会更改每个任务的 url,并始终使用 POST 动词。 Spring REST 使用不同的 HTTP 动词(GET、POST、DELETE)来区分操作。几次共享相同的网址。

例子:

@RestController
@RequestMapping("/users")
public class UsersController {
    @GetMapping
    public List<User> index() {...}

    @GetMapping("{id}")
    public User show(...) {...}

    @PostMapping
    public User create(...) {...}

    @PutMapping("{id}")
    public User update(...) {...}

    @DeleteMapping("{id}")
    public void delete(...) {...}
}

【讨论】:

    【解决方案2】:

    您的示例未遵循 REST API 的约定(例如,GET 用于检索,POST 用于创建,PUT 用于完全更新,PATCH 用于部分更新等),但这并不意味着您不能。正如上面其他人所说,您可能只是对这个术语感到困惑。 REST 是一种协议,它有很多关于服务使用的约定,如果你遵循这些约定,你可以说你的服务是 REST 或 RESTful。 此页面是在该领域为您提供辅导的简单最佳来源: https://restfulapi.net 更重要的是,当我们考虑您的示例时:https://restfulapi.net/http-methods/

    我有时也会检查一下。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-28
      • 2020-03-19
      • 1970-01-01
      • 2018-07-13
      • 2017-10-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多