欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring boot中 RestFull风格

程序员文章站 2022-07-15 15:42:11
...

第一种:

package com.rabbitmqdemo.demo;

import lombok.Data;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @auther 
 * @date 2020/1/9 17:13
 * @description
 */
@Data
class User {
    String name;
    int age;
    String address;

    public User(String name, int age, String address) {
        this.name = name;
        this.age = age;
        this.address = address;
    }

}

@RestController
public class DemoController {
	//http://localhost:8099/aa/zjhhhh/118/北京
    @RequestMapping("/aa/{name}/{age}/{address}")
    public String a(
            @PathVariable("name") String name,
            @PathVariable("age") int age,
            @PathVariable("address") String address
    ){
        User user = new User(name, age, address);
        return user.toString();
    }
}

第二种:

package com.rabbitmqdemo.demo;

import lombok.Data;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @auther 
 * @date 2020/1/9 17:13
 * @description
 */
@Data
class User {
    String name;
    Integer age;
    String address;

    public User(String name, Integer age, String address) {
        this.name = name;
        this.age = age;
        this.address = address;
    }

}

@RestController
public class DemoController {
    //http://localhost:8099/aa/zjhhhh/118/北京
    @RequestMapping("/aa/{name}/{age}/{address}")
    public String a(User user){
        return user.toString();
    }
}

如果url传入的参数可以用一个对象接收的话,可以简写。

相关标签: Spring boot