1. 程式人生 > >SpringBoot-(3)-RestController介面引數

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 + "日";
    }


}