增加请求地址前缀
package com.ruoyi.framework.config; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.PathMatchConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import com.ruoyi.common.annotation.ApiRestController; @Configuration public class ApiPrefixAutoConfiguration implements WebMvcConfigurer { /** * * <p> * 增加restApi前缀 * </p> * */ @Override public void configurePathMatch(PathMatchConfigurer configurer) { configurer.addPathPrefix("/app", c -> c.isAnnotationPresent(ApiRestController.class)); } }
package com.ruoyi.common.annotation; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import org.springframework.core.annotation.AliasFor; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @RestController @RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE) public @interface ApiRestController { @AliasFor(annotation = RequestMapping.class) String name() default ""; @AliasFor(annotation = RequestMapping.class) String[] value() default {}; @AliasFor(annotation = RequestMapping.class) String[] path() default {}; }
controller类中只需要加入@ApiRestController注解,请求地址就会添加"/app"
package com.ruoyi.xgzj.business.app.appController; import com.github.pagehelper.Page; import com.ruoyi.common.annotation.ApiRestController; import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.generator.ghbTestTable.domain.GhbTestTable; import com.ruoyi.generator.ghbTestTable.service.IGhbTestTableService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestBody; import java.util.HashMap; import java.util.List; import java.util.Map; import static com.ruoyi.common.utils.PageUtils.startPage; @ApiRestController public class appGhbTestController { @Autowired private IGhbTestTableService iGhbTestTableService; @GetMapping("/list") public AjaxResult list(@RequestBody GhbTestTable ghbTestTable){ Page<Object> objects = startPage(Integer.parseInt(ghbTestTable.getPageNum()), Integer.parseInt(ghbTestTable.getPageSize())); List<GhbTestTable> ghbTestTables = iGhbTestTableService.selectGhbTestTableList(ghbTestTable); Map data = new HashMap(); data.put("total",objects.getTotal()); data.put("list",ghbTestTables); return AjaxResult.success(data); } }
本文作者:岁月记忆
本文链接:https://www.cnblogs.com/huang2979127746/p/16356140.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步