java编写webapi_springBoot webapi示例源码(含数据库脚本)

本文通过一个简单的Java SpringBoot应用展示了如何创建RESTful Web API,实现对城市数据的增删查改操作。示例中包含对应的控制器类(CityController)代码,该类使用了@Autowired注解注入ICityService接口,提供了GET, POST, PUT, DELETE方法,对应于数据库中的城市ID查询、所有城市列表、创建城市、更新城市和删除城市等功能。" 81709475,7623089,使用Kruskal算法求解最小生成树,"['图论算法', 'ACM竞赛', '数据结构']
摘要由CSDN通过智能技术生成

【实例简介】

【实例截图】

cc760f7bc5b65ca36e667aa15393424f.png

261c8d5d08c40e7a260563b22891e911.png

【核心代码】

package com.spb.SpringBootDemo.rest;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.RestController;

import com.spb.SpringBootDemo.service.ICityService;

import com.spb.SpringBootDemo.vo.City;

@RestController

public class CityContorller {

@Autowired

private ICityService cityService;

@RequestMapping(value = "/api/city/{id}", method = RequestMethod.GET)

public City findOneCity(@PathVariable("id") Long id) {

return cityService.findCityById(id);

}

@RequestMapping(value = "/api/city", method = RequestMethod.GET)

public List findAllCity() {

return cityService.findAllCity();

}

@RequestMapping(value = "/api/city", method = RequestMethod.POST)

public void createCity(@RequestBody City city) {

cityService.saveCity(city);

}

@RequestMapping(value = "/api/city", method = RequestMethod.PUT)

public void modifyCity(@RequestBody City city) {

cityService.updateCity(city);

}

@RequestMapping(value = "/api/city/{id}", method = RequestMethod.DELETE)

public void modifyCity(@PathVariable("id") Long id) {

cityService.deleteCity(id);

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值