我的第一个springboot接口项目

  1. idea创建项目

在这里插入图片描述
勾选web-spring web依赖
在这里插入图片描述
创建新的类,拼接url
在这里插入图片描述
启动
@SpringBootApplication

postman调用接口http://localhost:8080/h1/h2
在这里插入图片描述

  1. 带有参数的接口
package com.example.demo;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/h1")
public class http {
    @RequestMapping("/h2")
    public String toweb(@RequestBody Test param){
        System.out.println(param.getAge()+"&&&");
        System.out.println(param.getName()+"****");
        System.out.println("sucess"+param);
        return "hello";
    }
}

Test是自定义的类,

package com.example.demo;

public class Test {
    int age;
    String name;

    @Override
    public String toString() {
        return "Test{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public String getName() {
        return name;
    }

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

postmen里面的json字段要和类的属性一致,否则获取到null,如果不加@RequestBody,则
可以获取到form-data和x-www-form-urlencoded

   @GetMapping("/goods/{id}")
    public Goods goods(@PathVariable(name = "id") Integer id) {
        Goods g = new Goods(id, "草莓", 6.5, new Date(), "好吃");
        return g;
    }
@RestController
@RequestMapping("/h1")
public class http {
    @RequestMapping("/h2/{id}")
    @ResponseBody
    public String toweb(@PathVariable(name = "id") Integer id){
        System.out.println(id+"&&&");
        System.out.println("sucess");
        return "543";
    }
}

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值