码迷,mamicode.com
首页 > 编程语言 > 详细

SpringBoot-(3)-RestController接口参数

时间:2018-11-20 00:03:15      阅读:790      评论:0      收藏:0      [点我收藏+]

标签:oca   ping   name   ota   count   atd   code   alt   .post   

一,无参接口:

    //无参接口
    @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 + "日";
    }


}

 

SpringBoot-(3)-RestController接口参数

标签:oca   ping   name   ota   count   atd   code   alt   .post   

原文地址:https://www.cnblogs.com/yangzigege/p/9986406.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!