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

【实例简介】

【实例截图】

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
    评论
好的,以下是一个简单的 Spring Boot 实现 API 接口多版本支持的示例代码。 首先,我们需要在 `pom.xml` 文件中添加 `spring-boot-starter-parent` 依赖和 `spring-boot-starter-web` 依赖。然后,在 `application.properties` 文件中添加一些配置: ``` # API 版本 api.version=v1 # v1 版本 API 的 URL 前缀 api.v1.path=/api/v1 # v2 版本 API 的 URL 前缀 api.v2.path=/api/v2 ``` 然后,我们创建一个 `VersionHandlerInterceptor` 拦截器来处理 API 版本: ```java import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class VersionHandlerInterceptor implements HandlerInterceptor { private static final String API_VERSION_HEADER = "Api-Version"; private static final String DEFAULT_API_VERSION = "v1"; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { String apiVersion = request.getHeader(API_VERSION_HEADER); if (apiVersion == null) { apiVersion = DEFAULT_API_VERSION; } request.setAttribute("apiVersion", apiVersion); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { } } ``` 该拦截器从请求头中获取 API 版本,如果没有指定则默认为 `v1`,并将其存储到请求属性中。 接下来,我们创建一个 `ApiVersionRequestMappingHandlerMapping` 请求映射处理器来支持多版本 API: ```java import org.springframework.core.annotation.AnnotationUtils; import org.springframework.util.StringUtils; import org.springframework.web.servlet.mvc.condition.RequestCondition; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; import java.lang.reflect.Method; public class ApiVersionRequestMappingHandlerMapping extends RequestMappingHandlerMapping { private static final String API_VERSION_ATTRIBUTE = "apiVersion"; @Override protected RequestCondition<ApiVersionRequestCondition> getCustomTypeCondition(Class<?> handlerType) { ApiVersion apiVersion = AnnotationUtils.findAnnotation(handlerType, ApiVersion.class); return createRequestCondition(apiVersion); } @Override protected RequestCondition<ApiVersionRequestCondition> getCustomMethodCondition(Method method) { ApiVersion apiVersion = AnnotationUtils.findAnnotation(method, ApiVersion.class); return createRequestCondition(apiVersion); } private RequestCondition<ApiVersionRequestCondition> createRequestCondition(ApiVersion apiVersion) { return apiVersion == null ? null : new ApiVersionRequestCondition(apiVersion.value()); } private static class ApiVersionRequestCondition implements RequestCondition<ApiVersionRequestCondition> { private final String apiVersion; public ApiVersionRequestCondition(String apiVersion) { this.apiVersion = apiVersion; } @Override public ApiVersionRequestCondition combine(ApiVersionRequestCondition other) { return new ApiVersionRequestCondition(other.apiVersion); } @Override public ApiVersionRequestCondition getMatchingCondition(HttpServletRequest request) { String apiVersion = (String) request.getAttribute(API_VERSION_ATTRIBUTE); if (!StringUtils.hasText(apiVersion)) { apiVersion = DEFAULT_API_VERSION; } return new ApiVersionRequestCondition(apiVersion.equals(this.apiVersion) ? this.apiVersion : null); } @Override public int compareTo(ApiVersionRequestCondition other, HttpServletRequest request) { String apiVersion = (String) request.getAttribute(API_VERSION_ATTRIBUTE); if (!StringUtils.hasText(apiVersion)) { apiVersion = DEFAULT_API_VERSION; } return apiVersion.compareTo(other.apiVersion); } } } ``` 该请求映射处理器通过 `ApiVersion` 注解获取 API 版本,然后根据 API 版本创建一个自定义请求条件 `ApiVersionRequestCondition`。 最后,我们可以使用 `@ApiVersion` 注解来指定 API 版本: ```java import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController @ApiVersion("v1") public class UserController { @GetMapping("/users") public String getUsers() { return "v1 users"; } } ``` 根据上述示例代码,我们可以轻松实现 Spring Boot 的 API 接口多版本支持。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值