栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot搭建swagger3和跨域项目

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot搭建swagger3和跨域项目

第一天学习记录及结果记录:

yml配置文件:
server: port: 8080 servlet: context-path: / spring: application: name: dasaiend datasource: url: “jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai” username: root password: 123456 driver-class-name: com.mysql.cj.jdbc.Driver mybatis: mapper-locations: classpath:mapping/*Mapper.xml type-aliases-package: com.example.module1.dealtask.entity

swagger3 maven配置:

io.springfox springfox-boot-starter 3.0.0

swagger3类的配置
package com.example.module1.swagger3pack; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.spi.documentationType; import springfox.documentation.spring.web.plugins.Docket; @Configuration public class SwaggerConfig { public Docket createRestapi(){ return new Docket(documentationType.SWAGGER_2) .pathMapping("/") .select() .apis(RequestHandlerSelectors.basePackage(“com.example.module1.dealtask”)) .paths(PathSelectors.any()) .build(); } }

跨域类的配置:
package com.example.module1.configbean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class MyWebMvcConfig implements WebMvcConfigurer { public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedHeaders("") .allowedMethods("") .maxAge(1800) .allowedOrigins("*"); } }

mpper.xml

insert into login(name,password) values(#{name},#{password})

ResponseCode文件
package com.example.module1.dealtask.entity.responsepack; public enum ResponseCode { SUCCESS(10001,“操作成功”), FAIL(10002,“操作成功”); private int code; private String msg; ResponseCode(int code,String msg){ this.code=code; this.msg=msg; } public int getCode() { return code; } public String getMsg() { return msg; } }

ResponseResult
package com.example.module1.dealtask.entity.responsepack; import lombok.Data; import java.io.Serializable; @Data public class ResponseResult { private int code; private String msg; private T Data; public ResponseResult(){ } public ResponseResult(int code,String msg){ this.code = code; this.msg = msg; } public static ResponseResult SUCCESS(){ return new ResponseResult(ResponseCode.SUCCESS.getCode(),ResponseCode.SUCCESS.getMsg()); } public static ResponseResult FAIL(){ return new ResponseResult(ResponseCode.FAIL.getCode(),ResponseCode.FAIL.getMsg()); } }

LoginController的配置
@RestController @RequestMapping("/loginController") public class LoginController { @Autowired private LoginService loginService; @ApiOperation(value = “插入数据”,response=ResponseResult.class) @PostMapping(value = “/insertLogin”,produces = “application/json;charset=UTF-8”) @ResponseBody public ResponseResult insertLogin(@RequestBody LoginDto loginDto){ ResponseResult resul= ResponseResult.SUCCESS(); int res=loginService.insertLogin(loginDto); resul.setData(res); return resul; } }
第一天的学习结束,坚持到底,继续加油!!!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/356925.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号