【Java】Spring boot快速上手(二):参数传递

【Java】Spring boot快速上手(二):参数传递_第1张图片

什么是参数传递:

例如:http://localhost:8080/sprsds/index?name=87123

 @RequestMapping(value = "/sprsds/index")
    @ResponseBody
    String eat(@RequestParam("name") String name) {
        System.out.println("test");
        return name;
    }

响应
在这里插入图片描述


get方式Url传参

|—@PathVariable 即:url/id/87123形式
|—@RequestParam 即:url?name=87123形式

@PathVariable

package com.example.demo.User;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller

public class index {
    @RequestMapping(value = "/sprsds/{name}")
    @ResponseBody String eat(@PathVariable("name") String name) {
        System.out.println("test");
        return name;
    }
}

响应
【Java】Spring boot快速上手(二):参数传递_第2张图片


POST方式传参

@RequestParam

package com.example.demo.User;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller

public class index {
    @RequestMapping(value = "/sprsds/index")
    @ResponseBody
    String eat(@RequestParam("name") String name) {
        System.out.println("test");
        return name;
    }
}

响应
【Java】Spring boot快速上手(二):参数传递_第3张图片


你可能感兴趣的:(Java,java,spring,boot,spring)