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配置:
<!-- swagger 3以后 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency>
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" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.module1.dealtask.mapper.LoginMapper"> <insert id="insertLogin" parameterType="com.example.module1.dealtask.LoginDto"> insert into login(name,password) values(#{name},#{password}) </insert> </mapper>
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<T> { 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<Integer> insertLogin(@RequestBody LoginDto loginDto){ ResponseResult<Integer> resul= ResponseResult.SUCCESS(); int res=loginService.insertLogin(loginDto); resul.setData(res); return resul; } }
第一天的学习结束,坚持到底,继续加油!!!
标签:code,springboot,ResponseResult,swagger3,msg,import,com,public,跨域 来源: https://www.cnblogs.com/ssgblogs/p/15477935.html