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传入的参数可以用一个对象接收的话,可以简写。
上一篇: SpringMVC的Restful风格