SpringBoot-(3)-RestController接口參數


一,無參接口:

    //無參接口
    @RequestMapping("/appSecret")
    public String secret() {
        return "EK125EKLNGKNELKGKGNKLEGNK87";
    }

  訪問接口

  

 

二,帶參接口:

 @RequestMapping("/serviceTime")
    public String time(@RequestParam(value = "local", required = true) String local) {
        System.out.println("local:"+local);
        return "2018-8-8 18:36:00";
    }

  訪問接口

  

  

 

三,多參接口

//多參接口,表單
    @RequestMapping("/register")
    public Account register(String username, String password) {
        Account user = new Account();
        user.setUsername(username);
        user.setPassword(password);
        return user;
    }

  訪問接口

  

 

四,json實例對象

//json實體對象
    @RequestMapping(value = "/addAccount", method = RequestMethod.POST)
    public Account addAccount(@RequestBody Account account) {
        System.out.print(account.getUsername());
        return account;
    }

  訪問接口:

  

 

五,路徑參數:

//路徑參數
    @RequestMapping(value="/searchAccountById/{id}",method = RequestMethod.POST)
    public String searchAccountById(@PathVariable("id") int id) {
        return "{id:"+id+"}";
    }
    @RequestMapping(value="/formatDate/{year}-{month}-{day}",method = RequestMethod.POST)
    public String formatDate(@PathVariable("year") int year, @PathVariable("month") int month, @PathVariable("day") int day) {
        return year + "年" + month + "月" + day + "日";
    }

  訪問接口

  

  

 

 

Controller代碼:

package com.example.demo.controllers;

import com.example.demo.domain.Account;
import org.springframework.web.bind.annotation.*;

/**
 * Created by zhang_guang_yang on 2018/11/18.
 */
@RestController
public class UserBusinessController {

    //無參接口
    @RequestMapping("/appSecret")
    public String secret() {
        return "EK125EKLNGKNELKGKGNKLEGNK87";
    }

    //帶參接口
    @RequestMapping("/serviceTime")
    public String time(@RequestParam(value = "local", required = true) String local) {
        System.out.println("local:"+local);
        return "2018-8-8 18:36:00";
    }

    //多參接口,表單
    @RequestMapping("/register")
    public Account register(String username, String password) {
        Account user = new Account();
        user.setUsername(username);
        user.setPassword(password);
        return user;
    }

    //json實體對象
    @RequestMapping(value = "/addAccount", method = RequestMethod.POST)
    public Account addAccount(@RequestBody Account account) {
        System.out.print(account.getUsername());
        return account;
    }

    //路徑參數
    @RequestMapping(value="/searchAccountById/{id}",method = RequestMethod.POST)
    public String searchAccountById(@PathVariable("id") int id) {
        return "{id:"+id+"}";
    }
    @RequestMapping(value="/formatDate/{year}-{month}-{day}",method = RequestMethod.POST)
    public String formatDate(@PathVariable("year") int year, @PathVariable("month") int month, @PathVariable("day") int day) {
        return year + "年" + month + "月" + day + "日";
    }


}

 

補充: Map類型多參數轉換,POST,GET接口的實現:

package com.ams.accountmanagementsystem.controllers;

import com.ams.accountmanagementsystem.models.TestUser;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
public class ApiTest {

    // 不帶參數
    @RequestMapping("/test_no_param")
    public String testNoParam() {
        return "success";
    }

    // 帶有一個參數
    @RequestMapping("/test_param")
    public String testParam(@RequestParam String param) {
        return "success, the param is: " + param;
    }

    // 帶多個參數
    @RequestMapping("/test_multiple_param")
    public String testMultipleParam(@RequestParam String name, @RequestParam String password) {
        return "success, the name is: " + name + " password is: " + password;
    }

    // map接受參數
    @RequestMapping("/test_param_map")
    public String testParamMap(@RequestParam Map map) {
        return "success, map: " + map;
    }

    // path參數
    @RequestMapping("/test/{path}")
    public String testPath(@PathVariable String path) {
        return "success, path: " + path;
    }

    // post帶一個參數
    @PostMapping("/test_post_param")
    public String postParam(@RequestBody String time) {
        return "success, time: " + time;
    }

    // post帶Map參數
    @PostMapping("/test_post_map_param")
    public String postMapParam(@RequestBody Map map) {
        return "success, map: " + map;
    }

    // post轉實體對象
    @PostMapping("/test_post_entity")
    public String postEntity(@RequestBody TestUser user) {
        return "success, entity " + user;
    }

    // POST GET
    @RequestMapping(value = "/test_get", method = RequestMethod.GET)
    public String testGetMethod() {
        return "get method";
    }

    @RequestMapping(value = "/test_post", method = RequestMethod.POST)
    public String testPostMethod() {
        return "post method";
    }

    @GetMapping("/test_get_method")
    public String testGet() {
        return "get method";
    }

    @PostMapping("/test_post_method")
    public String testPost() {
        return "post method";
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM