我的第一个springboot接口项目-创新互联
- idea创建项目
勾选web-spring web依赖
创建新的类,拼接url
启动
@SpringBootApplication
postman调用接口http://localhost:8080/h1/h2
- 带有参数的接口
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://scpingwu.com/article/cchpjg.html