Spring-boot实例代码

2019-02-25  本文已影响0人  简洁心飞
params、form-data 都可以使用get、post发送

package com.xinyue.one;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/hello")
    public String hello(){
        return  "你好我的中国🇨🇳";
    }

    @RequestMapping("/name")
    public String getName(){
        User user = new User();
        user.setId(1);
        user.setName("心飞");
        return user.getName();
    }
    @RequestMapping("/name1")
    public String getName1(@RequestParam("name") String name){
        /**
         * http://127.0.0.1:8080/user/name1?name=英雄联盟
         * params 使用get、post都可以发送
         */
        return name;
    }

    @RequestMapping(value = "/par1", method = RequestMethod.GET)
    public String reqPar1(@RequestParam("name") String name){
        return name;
    }

    @RequestMapping(value = "/par2", method = RequestMethod.GET)
    public String reqPar2(@RequestParam(value = "name", required = false) String name){
        if(null != name){
            return name;
        }else{
            return "未传入参数";
        }
    }

    @RequestMapping(value = "/par3", method = RequestMethod.GET)
    public String reqPar3(@RequestParam(value = "name", defaultValue = "心悦飞飞") String name){
        return name;
    }

    @RequestMapping(value = "/par4/{id}", method = RequestMethod.GET)
    public Integer reqPar4(@PathVariable("id") Integer id){
        return id;
    }
    @RequestMapping(value = "/{id}/par5", method = RequestMethod.GET)
    public Integer reqPar5(@PathVariable("id") Integer id){
        return id;
    }

    @RequestMapping(value = "/par6/{name}", method = RequestMethod.GET)
    public String reqPar4(@PathVariable("name") String name){
        return name;
    }
}
上一篇下一篇

猜你喜欢

热点阅读