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

<?xml version="1.0" encoding="UTF-8" ?> 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; } }
第一天的学习结束,坚持到底,继续加油!!!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值