HATEOAS Driven REST API Design Help

හෙනයා

Well-known member
  • May 23, 2014
    16,729
    16,833
    113
    Kottawa
    මචන්ලා.. ලොකු උදව්වක් ඕනේ.. HATEOAS එක්ක REST හදද්දී bidirectional relationships manage කරල relation links generate කරගන්න හැටි ගැන පොඩි advice වගයක් ඕනේ.. :sorry:

    example එකක් විදිහට මේ classes දෙකට bi-direction තියෙනවා කියල හිතුවොත්

    Student <-----> Batch
    Many To One​

    මම entity class දෙක ලිව්වේ මෙහෙමයි..

    PHP:
    class Student {
       Long id;
    
       @ManyToOne
       Batch batch;
    }


    PHP:
    class Batch {
       Long id;
    
       @OneToMany
       List<Student> students;
    }

    එතකොට මම JSON response එකේ relationship links generate කරද්දී මේ ක්‍රම දෙකෙන් මොකක්ද කරන්න ඕනේ..?

    1 වෙනි ක්‍රමය කරනවානම් StudentController class එකේ වෙනම method එකක් (GET) ලියන්න වෙනවා නේද අදාළ student ට අදාළ batch එක ගන්න? :confused:

    2 වෙනි ක්‍රමයේදී එහෙම ඕනේ වෙන්නේ නෑ නේද? හැබැයි එහෙම කරලා පස්සට maintain කරද්දී තව තව ප්‍රශ්න එයිද? :nerd:

    Student Response - Method 1

    PHP:
    "id": "1",
    "links": [
       {
          "rel": "self",
          "href": "http://localhost:8080/students/1"   
       },
       {
          "rel": "batch",
          "href": "http://localhost:8080/students/1/batches/1" 
       }
    ]

    Student Response - Method 2

    PHP:
    "id": "1",
    "links": [
       {
          "rel": "self",
          "href": "http://localhost:8080/students/1"   
       },
       {
          "rel": "batch",
          "href": "http://localhost:8080/batches/1" 
       }
    ]

    මෙතැනදී batch එක පැත්තෙන් ගත්තොත් එවන්න වෙන්නේ Resource Collection එකක් නෙහ්..(studentList) ? එතකොට අර විදිහට කරන්න බෑ නේද නිකන් student collection url එක එව්වොත් අදාළ batch එකට filter වෙන්නේ නැති නිසා.. :confused:

    Batch Response
    PHP:
    "id": "1",
    "links": [
       {
          "rel": "self",
          "href": "http://localhost:8080/batches/1"   
       },
       {
          "rel": "student",
          "href": "http://localhost:8080/batches/1/students" 
       }
    ]

    දන්නා කස්ටියගෙන් උදාව්.... :(:(:(
     

    kellsupun

    Member
    Feb 12, 2010
    61,910
    3,299
    0
    Disce aut Discede
    FfJyKm.gif
     

    හෙළයෙක්

    Well-known member
  • Apr 26, 2014
    48,467
    98,409
    113
    Uba kiyana de clear madi ban mata theruna widiyata answer ekak denan.



    Mata therena widiyata nan first method ekata join ekak dala liyana. DTO object ekak hadala eka ganawanan oya aula ena ekak ne.
     

    හෙනයා

    Well-known member
  • May 23, 2014
    16,729
    16,833
    113
    Kottawa
    හෙළයෙක්;22754453 said:
    Uba kiyana de clear madi ban mata theruna widiyata answer ekak denan.



    Mata therena widiyata nan first method ekata join ekak dala liyana. DTO object ekak hadala eka ganawanan oya aula ena ekak ne.

    එල මචන්.. එතකොට උබ කියන්නේ 1st method එක කරන්න කියල නෙහ්.. ? ;);)
     

    miyuru4u

    Well-known member
  • Jan 18, 2007
    31,888
    10,486
    113
    In the eyes of my girl
    හෙනයා;22754671 said:
    එල මචන්.. එතකොට උබ කියන්නේ 1st method එක කරන්න කියල නෙහ්.. ? ;);)

    ඔව් 1 කරල හරි යන් නැත්නම් 2 කරපන්
    bump
     

    isurueka

    Well-known member
  • Jul 26, 2008
    14,214
    347
    83
    මේක බලපන්

    http://www.baeldung.com/spring-data-rest-relationships

    GET එකක් වෙනම ලියනවා කියල අදහස් කරේ මොකක්ද? batch එක කියන්නේ student ගේ property එකක්නේ ඉතින් කොහොමත්?

    පලවෙනි ක්‍රමේ මෙන්න මෙහෙම තිබ්බ නම් හරි

    PHP:
    { 
          "rel": "batch", 
          "href": "http://localhost:8080/students/1/batch"  
    }

    මොකද මචන් student ගාව batches කියල collection එකක් නෑනේ? (one to many නිසා)

    දෙවෙනි ක්‍රමෙත් හරි.....REST වලදී මචං මේක විතරමයි හරි කියල දෙයක් නැහැ. REST කියන්නේ architectural style එකක් නිසයි ඒ.

    batch response එක ඔය තියන විදිහ හරිනේ......ඔය දාල තියෙන්නේ නිකන් student collection url එක නෙවෙයිනේ? එහෙම නැති නම් අර tutorial එකේ වගේ student list එක embed කරලා එවන්නත් පුළුවන්.

    දන්න විදිහට කිව්වේ.....වැරදි ඇති නම් නිවැරදි කරනු මැනවි. :yes:
     
    • Like
    Reactions: හෙනයා

    හෙනයා

    Well-known member
  • May 23, 2014
    16,729
    16,833
    113
    Kottawa
    මේක බලපන්

    http://www.baeldung.com/spring-data-rest-relationships

    GET එකක් වෙනම ලියනවා කියල අදහස් කරේ මොකක්ද? batch එක කියන්නේ student ගේ property එකක්නේ ඉතින් කොහොමත්?

    පලවෙනි ක්‍රමේ මෙන්න මෙහෙම තිබ්බ නම් හරි

    PHP:
    { 
          "rel": "batch", 
          "href": "http://localhost:8080/students/1/batch"  
    }

    මොකද මචන් student ගාව batches කියල collection එකක් නෑනේ? (one to many නිසා)

    දෙවෙනි ක්‍රමෙත් හරි.....REST වලදී මචං මේක විතරමයි හරි කියල දෙයක් නැහැ. REST කියන්නේ architectural style එකක් නිසයි ඒ.

    batch response එක ඔය තියන විදිහ හරිනේ......ඔය දාල තියෙන්නේ නිකන් student collection url එක නෙවෙයිනේ? එහෙම නැති නම් අර tutorial එකේ වගේ student list එක embed කරලා එවන්නත් පුළුවන්.

    දන්න විදිහට කිව්වේ.....වැරදි ඇති නම් නිවැරදි කරනු මැනවි. :yes:

    තැන්ක්ස් මචන්.. :cool::cool::cool: දැන් පොඩ්ඩක් මීටර් උනා වගේ.. :yes::yes:

    අර entity class එකට StudentController එකට සාමාන්‍යෙන් එන්න පුළුවන් methods ටික මිට අමතරව තව මොනාද කියල කියන්න පුලුවන්ද? ඒ කිව්වේ batch එක සහ batch link එක generate කරගන්න විදිහ.. ? :( (spring)


    PHP:
    @RequestMapping("/students")
    public class StudentController {
    
       @GetMapping
       public List<Student> findAll() { ... }
    
       @GetMapping(value = "{id}")
       public List<Student> findOne(@PathVariable Long id) { ... }
    
       @PostMapping
       public List<Student> save(@RequestBody Student student) { ... }
    
       @PutMapping(value = "{id}")
       public List<Student> update(@PathVariable Long id ) { ... }
    
       @DeleteMapping(value = "{id}")
       public List<Student> delete(@PathVariable Long id) { ... }
    
    }
     

    isurueka

    Well-known member
  • Jul 26, 2008
    14,214
    347
    83
    හෙනයා;22754945 said:
    තැන්ක්ස් මචන්.. :cool::cool::cool: දැන් පොඩ්ඩක් මීටර් උනා වගේ.. :yes::yes:

    අර entity class එකට StudentController එකට සාමාන්‍යෙන් එන්න පුළුවන් methods ටික මිට අමතරව තව මොනාද කියල කියන්න පුලුවන්ද? ඒ කිව්වේ batch එක සහ batch link එක generate කරගන්න විදිහ.. ? :( (spring)


    PHP:
    @RequestMapping("/students")
    public class StudentController {
    
       @GetMapping
       public List<Student> findAll() { ... }
    
       @GetMapping(value = "{id}")
       public List<Student> findOne(@PathVariable Long id) { ... }
    
       @PostMapping
       public List<Student> save(@RequestBody Student student) { ... }
    
       @PutMapping(value = "{id}")
       public List<Student> update(@PathVariable Long id ) { ... }
    
       @DeleteMapping(value = "{id}")
       public List<Student> delete(@PathVariable Long id) { ... }
    
    }

    අඩෝ සොරි බං මේක දැක්කේ අද.....වැඩේ ගොඩද?