我的第一个springboot接口项目-创新互联

  1. idea创建项目

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

东光ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18982081108(备注:SSL证书合作)期待与您的合作!

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";
    }
}

在这里插入图片描述

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧

当前题目:我的第一个springboot接口项目-创新互联
分享路径:https://www.cdcxhl.com/article26/cchpjg.html

成都网站建设公司_创新互联,为您提供品牌网站设计网站建设手机网站建设标签优化营销型网站建设App开发

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

绵阳服务器托管