我收到 nullPointer 错误,因为容器 ID 不可用。这可能是什么原因?我使用所有技术的最新版本。
[spring-projects/spring-boot]DockerCliComposePsResponse id 返回 null
回答
@hudaiapa88 请使用 Spring Boot 的发布版本而不是 RC1 版本重新测试。如果您仍然遇到问题,请分享您正在使用的 Docker Compose 版本以及重现问题的完整最小示例。您可以通过将其推送到 GitHub 上的单独存储库或将其压缩并附加到此问题来与我们共享。
项目文件= docker.zip 如您所见,我正在使用当前版本的技术。但我仍然遇到同样的错误。
@hudaiapa88 谢谢你提供的样本。下载并构建示例后,我可以运行./mvnw spring-boot:run
并看到 MySQL 容器和 Spring Boot 应用程序启动时没有错误。
请运行docker compose version
(或docker-compose version
)并使用显示的版本进行响应。另外,请将堆栈跟踪作为带有完整跟踪的文本而不是图像发布,以便我们可以全面了解错误。
C:\Windows\system32>docker-compose 版本 Docker Compose 版本 v2.22.0-desktop.2
[INFO]
[INFO] --- spring-boot:3.1.4:run (default-cli) @ docker ---
[INFO] Attaching agents: []
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.1.4)
2023-10-02T18:21:10.343+03:00 INFO 7564 --- [ main] com.uc.docker.DockerApplication : Starting DockerApplication using Java 17.0.3 with PID 7564 (C:\github_demo_projeler\docker\target\classes started by HUDAI in C:\github_demo_projeler\docker)
2023-10-02T18:21:10.347+03:00 INFO 7564 --- [ main] com.uc.docker.DockerApplication : No active profile set, falling back to 1 default profile: "default"
2023-10-02T18:21:10.439+03:00 INFO 7564 --- [ main] .s.b.d.c.l.DockerComposeLifecycleManager : Using Docker Compose file 'C:\github_demo_projeler\docker\compose.yaml'
2023-10-02T18:21:12.899+03:00 ERROR 7564 --- [ main] o.s.boot.SpringApplication : Application run failed
java.lang.NullPointerException: null
at java.base/java.util.Objects.requireNonNull(Objects.java:208) ~[na:na]
at java.base/java.util.ImmutableCollections.listFromArray(ImmutableCollections.java:190) ~[na:na]
at java.base/java.util.List.of(List.java:1047) ~[na:na]
at org.springframework.boot.docker.compose.core.DockerCliCommand.<init>(DockerCliCommand.java:57) ~[spring-boot-docker-compose-3.1.4.jar:3.1.4]
at org.springframework.boot.docker.compose.core.DockerCliCommand.<init>(DockerCliCommand.java:48) ~[spring-boot-docker-compose-3.1.4.jar:3.1.4]
at org.springframework.boot.docker.compose.core.DockerCliCommand$Inspect.<init>(DockerCliCommand.java:131) ~[spring-boot-docker-compose-3.1.4.jar:3.1.4]
at org.springframework.boot.docker.compose.core.DefaultDockerCompose.inspect(DefaultDockerCompose.java:90) ~[spring-boot-docker-compose-3.1.4.jar:3.1.4]
at org.springframework.boot.docker.compose.core.DefaultDockerCompose.getRunningServices(DefaultDockerCompose.java:80) ~[spring-boot-docker-compose-3.1.4.jar:3.1.4]
at org.springframework.boot.docker.compose.lifecycle.DockerComposeLifecycleManager.start(DockerComposeLifecycleManager.java:120) ~[spring-boot-docker-compose-3.1.4.jar:3.1.4]
at org.springframework.boot.docker.compose.lifecycle.DockerComposeListener.onApplicationEvent(DockerComposeListener.java:53) ~[spring-boot-docker-compose-3.1.4.jar:3.1.4]
at org.springframework.boot.docker.compose.lifecycle.DockerComposeListener.onApplicationEvent(DockerComposeListener.java:35) ~[spring-boot-docker-compose-3.1.4.jar:3.1.4]
at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:174) ~[spring-context-6.0.12.jar:6.0.12]
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:167) ~[spring-context-6.0.12.jar:6.0.12]
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:145) ~[spring-context-6.0.12.jar:6.0.12]
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:133) ~[spring-context-6.0.12.jar:6.0.12]
at org.springframework.boot.context.event.EventPublishingRunListener.multicastInitialEvent(EventPublishingRunListener.java:136) ~[spring-boot-3.1.4.jar:3.1.4]
at org.springframework.boot.context.event.EventPublishingRunListener.contextLoaded(EventPublishingRunListener.java:98) ~[spring-boot-3.1.4.jar:3.1.4]
at org.springframework.boot.SpringApplicationRunListeners.lambda$contextLoaded$4(SpringApplicationRunListeners.java:72) ~[spring-boot-3.1.4.jar:3.1.4]
at java.base/java.lang.Iterable.forEach(Iterable.java:75) ~[na:na]
at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:118) ~[spring-boot-3.1.4.jar:3.1.4]
at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:112) ~[spring-boot-3.1.4.jar:3.1.4]
at org.springframework.boot.SpringApplicationRunListeners.contextLoaded(SpringApplicationRunListeners.java:72) ~[spring-boot-3.1.4.jar:3.1.4]
at org.springframework.boot.SpringApplication.prepareContext(SpringApplication.java:419) ~[spring-boot-3.1.4.jar:3.1.4]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) ~[spring-boot-3.1.4.jar:3.1.4]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1309) ~[spring-boot-3.1.4.jar:3.1.4]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1298) ~[spring-boot-3.1.4.jar:3.1.4]
at com.uc.docker.DockerApplication.main(DockerApplication.java:10) ~[classes/:na]
@斯科特弗雷德里克
@scottfrederick 那么你推荐我使用哪个 docker 版本?
@scottfrederick
C:\Windows\system32>docker-compose --version Docker Compose 版本 v2.17.2
我安装了这个版本,但仍然出现同样的错误。
@hudaiapa88我认为你的环境和我正在测试的环境之间仍然存在一些不同。如果您可以在示例项目中运行这些命令并显示输出,将会很有帮助:
docker compose version
docker compose up -d
docker compose ps --format=json
PS C:\github_demo_projeler\modulith> docker compose version
Docker Compose version v2.19.0
PS C:\github_demo_projeler\modulith> docker compose up -d
[+] Running 1/0
✔ Container modulith-db-1 Running 0.0s
PS C:\github_demo_projeler\modulith> docker compose ps --format=json
[{"ID":"6e415c776da188a89658ae1afc5e6f5707272559b90eadc3a01d43d95cb498e3","Name":"modulith-db-1","Image":"postgres","Command":"docker-entrypoint.sh postgres","Project":"modulith","Serv
ice":"db","Created":1696277752,"State":"running","Status":"Up 2 hours","Health":"","ExitCode":0,"Publishers":[{"URL":"0.0.0.0","TargetPort":5432,"PublishedPort":5432,"Protocol":"tcp"}]}]
@scottfrederick 我运行了它,这就是结果
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.1.4)
2023-10-03T01:07:36.871+03:00 INFO 16936 --- [ main] com.uc.modulith.ModulithApplication : Starting ModulithApplication using Java 17.0.3 with PID 16936 (C:\github_demo_projeler\modulith\target\classes started by HUDAI in C:\github_demo_projeler\modulith)
2023-10-03T01:07:36.875+03:00 INFO 16936 --- [ main] com.uc.modulith.ModulithApplication : No active profile set, falling back to 1 default profile: "default"
2023-10-03T01:07:36.995+03:00 INFO 16936 --- [ main] .s.b.d.c.l.DockerComposeLifecycleManager : Using Docker Compose file 'C:\github_demo_projeler\modulith\compose.yaml'
2023-10-03T01:07:39.411+03:00 ERROR 16936 --- [ main] o.s.boot.SpringApplication : Application run failed
java.lang.NullPointerException: null
at java.base/java.util.Objects.requireNonNull(Objects.java:208) ~[na:na]
at java.base/java.util.ImmutableCollections.listFromArray(ImmutableCollections.java:190) ~[na:na]
at java.base/java.util.List.of(List.java:1047) ~[na:na]
at org.springframework.boot.docker.compose.core.DockerCliCommand.<init>(DockerCliCommand.java:57) ~[spring-boot-docker-compose-3.1.4.jar:3.1.4]
at org.springframework.boot.docker.compose.core.DockerCliCommand.<init>(DockerCliCommand.java:48) ~[spring-boot-docker-compose-3.1.4.jar:3.1.4]
at org.springframework.boot.docker.compose.core.DockerCliCommand$Inspect.<init>(DockerCliCommand.java:131) ~[spring-boot-docker-compose-3.1.4.jar:3.1.4]
at org.springframework.boot.docker.compose.core.DefaultDockerCompose.inspect(DefaultDockerCompose.java:90) ~[spring-boot-docker-compose-3.1.4.jar:3.1.4]
at org.springframework.boot.docker.compose.core.DefaultDockerCompose.getRunningServices(DefaultDockerCompose.java:80) ~[spring-boot-docker-compose-3.1.4.jar:3.1.4]
at org.springframework.boot.docker.compose.lifecycle.DockerComposeLifecycleManager.start(DockerComposeLifecycleManager.java:120) ~[spring-boot-docker-compose-3.1.4.jar:3.1.4]
at org.springframework.boot.docker.compose.lifecycle.DockerComposeListener.onApplicationEvent(DockerComposeListener.java:53) ~[spring-boot-docker-compose-3.1.4.jar:3.1.4]
at org.springframework.boot.docker.compose.lifecycle.DockerComposeListener.onApplicationEvent(DockerComposeListener.java:35) ~[spring-boot-docker-compose-3.1.4.jar:3.1.4]
at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:174) ~[spring-context-6.0.12.jar:6.0.12]
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:167) ~[spring-context-6.0.12.jar:6.0.12]
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:145) ~[spring-context-6.0.12.jar:6.0.12]
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:133) ~[spring-context-6.0.12.jar:6.0.12]
at org.springframework.boot.context.event.EventPublishingRunListener.multicastInitialEvent(EventPublishingRunListener.java:136) ~[spring-boot-3.1.4.jar:3.1.4]
at org.springframework.boot.context.event.EventPublishingRunListener.contextLoaded(EventPublishingRunListener.java:98) ~[spring-boot-3.1.4.jar:3.1.4]
at org.springframework.boot.SpringApplicationRunListeners.lambda$contextLoaded$4(SpringApplicationRunListeners.java:72) ~[spring-boot-3.1.4.jar:3.1.4]
at java.base/java.lang.Iterable.forEach(Iterable.java:75) ~[na:na]
at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:118) ~[spring-boot-3.1.4.jar:3.1.4]
at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:112) ~[spring-boot-3.1.4.jar:3.1.4]
at org.springframework.boot.SpringApplicationRunListeners.contextLoaded(SpringApplicationRunListeners.java:72) ~[spring-boot-3.1.4.jar:3.1.4]
at org.springframework.boot.SpringApplication.prepareContext(SpringApplication.java:419) ~[spring-boot-3.1.4.jar:3.1.4]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) ~[spring-boot-3.1.4.jar:3.1.4]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1309) ~[spring-boot-3.1.4.jar:3.1.4]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1298) ~[spring-boot-3.1.4.jar:3.1.4]
at com.uc.modulith.ModulithApplication.main(ModulithApplication.java:10) ~[classes/:na]
Disconnected from the target VM, address: '127.0.0.1:56546', transport: 'socket'
Process finished with exit code 1
谢谢。这个 JSON 对我来说看起来不错。
我能想到的唯一解释是 shell 中的路径与运行应用程序时的路径不同,因此应用程序使用不同版本的 Docker Compose。您可以通过设置断点调试应用程序org.springframework.boot.docker.compose.core.DockerCliCommand.deserialize(String)
并查看其中的 json 来测试这个理论。
我发现了同样的问题。
目前docker compose cli版本已经更新,预计可以通过附加--no-trunc
flag来解决。
- 更新版本:https://github.com/docker/compose/releases/tag/v2.23.0
- 相关pr:https://github.com/docker/compose/pull/11038
--no-trunc
旗帜
$ docker compose ps --format=json
{...,"ID":"d034853e71db","Image":"postgres:latest",...}
带--no-trunc
旗帜
$ docker compose ps --format=json --no-trunc
{...,"ID":"d034853e71dbb0326a473cfa9ca179419334f6d31451ca507b677e1582c1af1c","Image":"postgres:latest",...}
我认为在以下位置添加“--no-truc”可能会有所帮助ComposePs
/**
* The {@code docker compose ps} command.
*/
static final class ComposePs extends DockerCliCommand<List<DockerCliComposePsResponse>> {
ComposePs() {
super(Type.DOCKER_COMPOSE, DockerCliComposePsResponse.class, true, "ps", "--format=json", "--no-trunc");
}
}
@bossm0n5t3r您所描述的似乎是一个单独的但可能相关的问题。 @hudaiapa88 使用的是早期版本的 Docker Compose,问题似乎是 ID 根本没有被解析。我无法使用任何版本的 Docker Compose 重现该问题。
如果使用 Docker Compose 2.23.0 时 ID 截断导致问题,您介意为此创建一个新问题吗?
@scottfrederick 好的,我将整理内容并创建一个新内容。谢谢你!
感谢您创建https://github.com/spring-projects/spring-boot/issues/37982,@bossm0n5t3r。
Docker Compose 插件v2.21包含https://github.com/docker/compose/pull/10918,它破坏了与该插件以前版本的兼容性。 Spring Boot 需要进行调整以支持新旧输出格式。
虽然这个输出格式确实发生了变化,但它并没有像我最初想象的那样破坏 Spring Boot 对响应的解析。我仍然无法使用任何版本的 Docker Compose 重现此问题。
@hudaiapa88 请使用上面提到的安迪的建议,并让我们知道向您显示的信息。
@斯科特弗雷德里克
json={"Command":"\"docker-entrypoint.sh mysqld\"","CreatedAt":"2023-10-25 22:09:51 +0300 +03","ExitCode":0,"Health":"","ID":"70e4b8a107d5c7ab7fe906541d305de20ef211b93ab1bb9de8dde10b5e1f79fa","Image":"mysql:latest","Labels":"desktop.docker.io/ports/3306/tcp=0.0.0.0:0,com.docker.compose.container-number=1,com.docker.compose.project.working_dir=C:\\github_demo_projeler\\docker,com.docker.compose.image=sha256:2d9aad1b5856bbb040d01e4acddaaad74b5fbd7f620dac33e4e58ecc5d0aa86d,com.docker.compose.oneoff=False,com.docker.compose.project=docker,com.docker.compose.project.config_files=C:\\github_demo_projeler\\docker\\compose.yaml,com.docker.compose.service=mysql,com.docker.compose.version=2.22.0,com.docker.compose.config-hash=1264e8a0ae23410aa71b40347bda0b54a786b98a71ba11b10bf9624a82de8c05,com.docker.compose.depends_on=","LocalVolumes":"1","Mounts":"94d6844634f6e6a6099eaab3a6021c7103990c1c1a0c59e35232cb9722c759dd","Name":"docker-mysql-1","Names":"docker-mysql-1","Networks":"docker_default","Ports":"33060/tcp, 0.0.0.0:60081-\u003e3306/tcp","Publishers":[{"URL":"0.0.0.0","TargetPort":3306,"PublishedPort":60081,"Protocol":"tcp"},{"URL":"","TargetPort":33060,"PublishedPort":0,"Protocol":"tcp"}],"RunningFor":"2 minutes ago","Service":"mysql","Size":"0B","State":"running","Status":"Up 2 minutes"}
在这个 json 之后我收到这样的错误
java.lang.NullPointerException: null
at java.base/java.util.Objects.requireNonNull(Objects.java:208) ~[na:na]
at java.base/java.util.ImmutableCollections.listFromArray(ImmutableCollections.java:190) ~[na:na]
at java.base/java.util.List.of(List.java:1047) ~[na:na]
at org.springframework.boot.docker.compose.core.DockerCliCommand.<init>(DockerCliCommand.java:57) ~[spring-boot-docker-compose-3.1.4.jar:3.1.4]
at org.springframework.boot.docker.compose.core.DockerCliCommand.<init>(DockerCliCommand.java:48) ~[spring-boot-docker-compose-3.1.4.jar:3.1.4]
at org.springframework.boot.docker.compose.core.DockerCliCommand$Inspect.<init>(DockerCliCommand.java:131) ~[spring-boot-docker-compose-3.1.4.jar:3.1.4]
at org.springframework.boot.docker.compose.core.DefaultDockerCompose.inspect(DefaultDockerCompose.java:90) ~[spring-boot-docker-compose-3.1.4.jar:3.1.4]
at org.springframework.boot.docker.compose.core.DefaultDockerCompose.getRunningServices(DefaultDockerCompose.java:80) ~[spring-boot-docker-compose-3.1.4.jar:3.1.4]
at org.springframework.boot.docker.compose.lifecycle.DockerComposeLifecycleManager.start(DockerComposeLifecycleManager.java:120) ~[spring-boot-docker-compose-3.1.4.jar:3.1.4]
at org.springframework.boot.docker.compose.lifecycle.DockerComposeListener.onApplicationEvent(DockerComposeListener.java:53) ~[spring-boot-docker-compose-3.1.4.jar:3.1.4]
at org.springframework.boot.docker.compose.lifecycle.DockerComposeListener.onApplicationEvent(DockerComposeListener.java:35) ~[spring-boot-docker-compose-3.1.4.jar:3.1.4]
at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:174) ~[spring-context-6.0.12.jar:6.0.12]
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:167) ~[spring-context-6.0.12.jar:6.0.12]
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:145) ~[spring-context-6.0.12.jar:6.0.12]
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:133) ~[spring-context-6.0.12.jar:6.0.12]
at org.springframework.boot.context.event.EventPublishingRunListener.multicastInitialEvent(EventPublishingRunListener.java:136) ~[spring-boot-3.1.4.jar:3.1.4]
at org.springframework.boot.context.event.EventPublishingRunListener.contextLoaded(EventPublishingRunListener.java:98) ~[spring-boot-3.1.4.jar:3.1.4]
at org.springframework.boot.SpringApplicationRunListeners.lambda$contextLoaded$4(SpringApplicationRunListeners.java:72) ~[spring-boot-3.1.4.jar:3.1.4]
at java.base/java.lang.Iterable.forEach(Iterable.java:75) ~[na:na]
at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:118) ~[spring-boot-3.1.4.jar:3.1.4]
at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:112) ~[spring-boot-3.1.4.jar:3.1.4]
at org.springframework.boot.SpringApplicationRunListeners.contextLoaded(SpringApplicationRunListeners.java:72) ~[spring-boot-3.1.4.jar:3.1.4]
at org.springframework.boot.SpringApplication.prepareContext(SpringApplication.java:419) ~[spring-boot-3.1.4.jar:3.1.4]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) ~[spring-boot-3.1.4.jar:3.1.4]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1309) ~[spring-boot-3.1.4.jar:3.1.4]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1298) ~[spring-boot-3.1.4.jar:3.1.4]
at com.uc.docker.DockerApplication.main(DockerApplication.java:10) ~[classes/:na]
Disconnected from the target VM, address: '127.0.0.1:60096', transport: 'socket'
Process finished with exit code 1
我不明白发生了什么,因为 json 似乎解码正确。
@hudaiapa88你可以尝试在DefaultDockerCompose.inspect
方法中添加一个断点吗?该异常似乎表明该ids
列表包含空值:
private Map<String, DockerCliInspectResponse> inspect(List<DockerCliComposePsResponse> runningPsResponses) {
List<String> ids = runningPsResponses.stream().map(DockerCliComposePsResponse::id).toList();
List<DockerCliInspectResponse> inspectResponses = this.cli.run(new DockerCliCommand.Inspect(ids));
return inspectResponses.stream().collect(Collectors.toMap(DockerCliInspectResponse::id, Function.identity()));
}
这意味着其中一条DockerCliComposePsResponse
记录可能有一个null
ID。我仍然不清楚为什么会这样,因为 json 似乎对我来说解析得很好。
@hudaiapa88 另一件事。你能检查你的类路径上的 Jackson 版本吗?有没有可能您使用的版本与我们不同?
@philwebb 命令属性中出现空引用
这真的很令人困惑,因为那null
应该是 ID 的集合。我真的不明白它是怎么回事,null
因为它应该在这里创建,并且该toList()
方法永远不应该返回null
。
您的项目或设置有什么不寻常的地方吗?您是否尝试过从 start.spring.io 创建的新项目来看看是否有同样的问题?
也许您可以进一步调试堆栈并尝试弄清楚如何DockerCliCommand.Inspect
使用 ids 创建新实例null
。
如果可以的话,可以在 GitHub 上分享一下你的项目吗?
docker.zip @philwebb 当我遇到第一个错误时,我从 start.spring.io 创建了这个项目。然后我在我创建的项目中遇到了这个错误。
runningPsResponses
您能给我们看一下其中包含的内容的屏幕截图org.springframework.boot.docker.compose.core.DefaultDockerCompose#getRunningServices
吗?
你好,我有同样的问题。似乎runningPsResponses
有截断的 ID 和inspected
没有截断的 constais ID。
@aalbiach-dkt 听起来更像https://github.com/spring-projects/spring-boot/issues/37982而不是本期中描述的问题
@philwebb,您好,我想我已经找到了问题,我遇到了与此相同的错误。显然, spring-docker-compose 使用 Fasterxml.Jackson ,它通过首先将所有字母小写来反序列化字段,然后使用系统区域设置语言将它们小写。因此,“docker -ps”响应中的 ID 字段被小写为 ıd,而不是土耳其语中的 id。我将操作系统语言切换为英语并解决了问题。这不是一个看似合理的解决方案,但至少找到了问题。
因此,如果有一种方法可以强制杰克逊使用英语而不改变操作系统语言,我很高兴听到。
@anil-ilgin 非常感谢您的指点。确实是这样!
该测试失败:
@Test
void shouldBeLocaleAgnostic() {
Locale locale = Locale.getDefault();
try {
String json = """
[{"ID":"6e415c776da188a89658ae1afc5e6f5707272559b90eadc3a01d43d95cb498e3","Name":"modulith-db-1","Image":"postgres","Command":"docker-entrypoint.sh postgres","Project":"modulith",
"Service":"db","Created":1696277752,"State":"running","Status":"Up 2 hours","Health":"","ExitCode":0,"Publishers":[{"URL":"0.0.0.0","TargetPort":5432,"PublishedPort":5432,"Protocol":"tcp"}]}]
""";
Locale.setDefault(Locale.forLanguageTag("tr-TR"));
List<DockerCliComposePsResponse> response = DockerJson.deserializeToList(json,
DockerCliComposePsResponse.class);
assertThat(response).hasSize(1);
assertThat(response.get(0).id())
.isEqualTo("6e415c776da188a89658ae1afc5e6f5707272559b90eadc3a01d43d95cb498e3");
}
finally {
Locale.setDefault(locale);
}
}
因此,如果有一种方法可以强制杰克逊使用英语而不改变操作系统语言,我很高兴听到。
有,我们会解决这个问题。