Springboot快速构建restful接口

demo目录结构如下:


这里用到了lombok,在pom文件中引入依赖

<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<version>1.16.10</version>
		</dependency>

在Intellij idea中快速构建Springboot web工程,

创建User类:

@Data
@NoArgsConstructor
public class User {
    private String username;
    private String password;
    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }
}
创建UserController类:

@RestController
public class UserController {
    @RequestMapping("/user")
    public User helloUser(@RequestParam(value="username",defaultValue = "tomcmd") String username,
                          @RequestParam(value = "password",defaultValue = "123456") String password){
        return new User(username,password);
    }
}

启动类代码,这里不需要更改

@SpringBootApplication
public class Demo01Application {

	public static void main(String[] args) {
		SpringApplication.run(Demo01Application.class, args);
	}
}
配置文件:application.properties中可以配置启动端口

server.port=8000
最后,启动工程,浏览器中访问localhost:8000/user?username=root&password=123,结果如下:





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值