Home  >  Q&A  >  body text

java - REST API命名问题请教

雇员类

public User{
    int id;//key
    int code;//员工编号 唯一
    String name;
    ...
}

api

  1. GET /employee/{id} 根据ID获取人员

  2. GET /employee/{code}

如上:code也是唯一的,如果这么命名是会混淆,应该如何命名?

怪我咯怪我咯2743 days ago568

reply all(3)I'll reply

  • 阿神

    阿神2017-04-18 10:54:41

    /employee/code/{code}

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:54:41

    Getting the only user is /employee/{id}. What should be obtained through this URL is the user entity object.
    For the properties inside the entity object: code . Should not be obtained by exposing the URL.

    The first step when designing RESTURL is resource abstraction. It is necessary to clearly distinguish which resources can be extracted and design a unique URL. What can’t be done

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:54:41

    RESTful resources generally use plural numbers. I think code can also be used as a search condition: /employees/search?code=xxx. Of course, code can also be obtained and changed as a resource of employee, but when searching for employee through code, it should be Belongs to the search function.

    reply
    0
  • Cancelreply