Skip to content

Commit

Permalink
GitHub json 响应的字段名样式统一配置为 SnakeCase
Browse files Browse the repository at this point in the history
  • Loading branch information
Lixuhuilll committed Dec 11, 2023
1 parent cd6a1bd commit e01f95a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
22 changes: 18 additions & 4 deletions src/main/java/plus/maa/backend/config/HttpInterfaceConfig.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package plus.maa.backend.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.codec.json.Jackson2JsonDecoder;
import org.springframework.http.codec.json.Jackson2JsonEncoder;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.web.reactive.function.client.ExchangeStrategies;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.support.WebClientAdapter;
Expand All @@ -13,14 +18,23 @@ public class HttpInterfaceConfig {

@Bean
GithubRepository githubRepository() {

ObjectMapper mapper = Jackson2ObjectMapperBuilder.json()
.propertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE)
.build();

WebClient client = WebClient.builder()
.baseUrl("https://api.github.com")
.exchangeStrategies(ExchangeStrategies
.builder()
.codecs(codecs -> codecs
.defaultCodecs()
// 最大 20MB
.maxInMemorySize(20 * 1024 * 1024))
.codecs(codecs -> {
codecs.defaultCodecs()
.jackson2JsonEncoder(new Jackson2JsonEncoder(mapper));
codecs.defaultCodecs()
.jackson2JsonDecoder(new Jackson2JsonDecoder(mapper));
// 最大 20MB
codecs.defaultCodecs().maxInMemorySize(20 * 1024 * 1024);
})
.build())
.defaultHeaders(headers -> {
headers.add("Accept", "application/vnd.github+json");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package plus.maa.backend.repository.entity.github;

import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

Expand All @@ -12,7 +10,6 @@
* created on 2022/12/23
*/
@Data
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class GithubContent {

// 文件名
Expand Down

0 comments on commit e01f95a

Please sign in to comment.