RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
我的第一个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接口项目-创新互联
文章URL:http://sczitong.cn/article/cchpjg.html