在些模块中汇总了一些web开发常用的配置和功能。
涉及的模块
- springboot-common-config, 端口号:17000
Springboot框架web开发常用功能
Restful接口定义
查询参数
@Data
public class QueryParam {
private String key;
private String value;
}
Controller实现
RestfulController.java,主要汇总一些常用的restful的写法
@Slf4j
@RestController
@RequestMapping("/api/load")
public class RestfulController {
/*默认 required = true*/
@GetMapping("/v1/getMapping")
public BaseResponse<String> getMapping( @RequestParam(value="username", required = false) String name){
log.info("getMapping:{}", name);
return BaseResponse.success(name);
}
/*这处必须用 @PathVariable 注解*/
@GetMapping("/v1/pathVariable/{name}")
public BaseResponse pathVariable(@PathVariable("name") String name){
log.info("getMapping:{}", name);
return BaseResponse.success(name);
}
/*默认 required = true*/
@PostMapping(value = "/v1/postMapping")
public BaseResponse postMapping(@RequestBody QueryParam queryParam){
log.info("getMapping:{}", queryParam);
return BaseResponse.success(queryParam);
}
@RequestMapping(value = "/v1/allRequestMapping/{name}")
public BaseResponse allRequestMapping(@PathVariable("name") String name){
log.info("getMapping:{}", name);
return BaseResponse.success(name);
}
}
RestTemplate客户端配置
Maven依赖
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.14</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.2.3</version>
</dependency>
Bean配置
配置java-ben
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
return restTemplate;
}
}
修改application.properties
spring.profiles.active = dev
spring.application.name=springbootCommonConfig
server.port=17000
#自定义配置服务端
service-url.service = http://www.baidu.com
调用http接口
@Slf4j
@RestController
@RequestMapping("/api/rmihttp")
public class TemplateController {
@Autowired
private RestTemplate restTemplate;
@Value("${service-url.service}")
private String serviceUrl;
@GetMapping("/v1/hello-content")
public BaseResponse<String> loadHelloContent(String uuid){
String result = restTemplate.getForObject(serviceUrl + "/s?wd=springboot", String.class, uuid);
return BaseResponse.success(result);
}
}
Swagger配置
修改application-dev.properties
以下全是自定义的属性,需要有一个相应的配置类,如下:
#swagger
springdoc.api-docs.enabled=true
springdoc.api-docs.path=/api-schema
swagger-config.group = default-group
swagger-config.description= The following is a restful-api list of {} application, and you can browse or test them to determine if they are working as you expect.
swagger-config.version=V1.0
swagger-config.urlPattern=/**
swagger-config.base-package=com.korgs
swagger-config.authorization-key-name=token
swagger-config.wiki = https://korgs.blog.csdn.net/
Swagger配置类
@Configuration
public class OpenAPIConfig {
@Autowired
private SwaggerProperties swaggerProperties;
@Value("${spring.application.name}")
private String applicationName;
@Bean
public OpenAPI openAPI() {
return new OpenAPI()
.info(new Info()
.title(applicationName)
.description(StrUtil.format(swaggerProperties.getDescription(), applicationName))
.version(swaggerProperties.getVersion()))
.externalDocs(new ExternalDocumentation()
.description("See details documentation, please click here!")
.url(swaggerProperties.getWiki()));
}
@Bean
public GroupedOpenApi applicationRestfulApi() {
return GroupedOpenApi.builder()
.group(swaggerProperties.getGroup())
.packagesToScan(swaggerProperties.getBasePackage().split(","))
.pathsToMatch(swaggerProperties.getUrlPattern())
.build();
}
@Data
@Component
@ConfigurationProperties(prefix = "swagger-config")
public static class SwaggerProperties{
private String group;
private String description;
private String version;
private String basePackage;
private String authorizationKeyName;
private String urlPattern;
private String wiki;
}
}