「Spring MVC」@RestControllerでjsonデータを提供するサンプル

書式
@RestController
@RequestMapping(xxx)
public class Sample
サンプルコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
@RestController
@RequestMapping(value="/students")
public class StudentRestController {
//全てのデータ取得
@RequestMapping(value="/{student}", method=RequestMethod.GET)
public Student getStudent(@PathVariable Long student) {
// some code
}
//customersデータ取得
@RequestMapping(value="/{student}/customers", method=RequestMethod.GET)
List<Customer> getStudentCustomers(@PathVariable Long student) {
// some code
}
//データ削除
@RequestMapping(value="/{student}", method=RequestMethod.DELETE)
public Student deleteUser(@PathVariable Long student) {
// some code
}
}
@RestController @RequestMapping(value="/students") public class StudentRestController { //全てのデータ取得 @RequestMapping(value="/{student}", method=RequestMethod.GET) public Student getStudent(@PathVariable Long student) { // some code } //customersデータ取得 @RequestMapping(value="/{student}/customers", method=RequestMethod.GET) List<Customer> getStudentCustomers(@PathVariable Long student) { // some code } //データ削除 @RequestMapping(value="/{student}", method=RequestMethod.DELETE) public Student deleteUser(@PathVariable Long student) { // some code } }
@RestController
@RequestMapping(value="/students")
public class StudentRestController {

    //全てのデータ取得
    @RequestMapping(value="/{student}", method=RequestMethod.GET)
    public Student getStudent(@PathVariable Long student) {
        // some code
    }
      //customersデータ取得
    @RequestMapping(value="/{student}/customers", method=RequestMethod.GET)
    List<Customer> getStudentCustomers(@PathVariable Long student) {
        // some code
    }
      //データ削除
    @RequestMapping(value="/{student}", method=RequestMethod.DELETE)
    public Student deleteUser(@PathVariable Long student) {
        // some code
    }

}

 

SpringMVC

Posted by arkgame