【问题标题】:how do I receive an empty response in micronaut declarative client?如何在 micronaut 声明式客户端中收到空响应?
【发布时间】:2021-10-27 09:51:00
【问题描述】:

我正在向服务器发送一个PATCH 请求,它给了我一个 200 并且没有正文(完全可以接受)但是我不知道如何配置 micronaut @Patch 注释来处理这个问题。无论我尝试什么,它都会尝试将空响应解码为 JSON(至少我认为这就是它正在做的事情,因为它会给出意外错误,而不是当正文错误时出现的正常杰克逊错误)。我在 micronaut 文档中找不到有关如何执行此操作的任何内容。

【问题讨论】:

  • 您能否提供代码示例来说明您当前的尝试?它不必是完全相同的代码(如果它包含敏感数据),但可以通过最小的可行示例重现问题。
  • 与您正在调用的端点关联的响应的内容类型是什么?

标签: micronaut micronaut-client


【解决方案1】:

我正在向服务器发送 PATCH 请求,服务器返回 200 和 没有身体(完全可以接受)但我不知道如何 配置 micronaut 的 @Patch 注解来处理这个

我认为这将适用于 @Patch,就像它适用于其他动词一样。

在https://github.com/jeffbrown/patchwithemptyresponse查看项目。

server/src/main/java/server/DemoController.java

package server;

import io.micronaut.http.HttpResponse;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Patch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Controller("/demo")
public class DemoController {
    static final Logger log = LoggerFactory.getLogger(DemoController.class);

    @Patch
    HttpResponse doSomething() {
        log.info("Returning an empty response with status code 200");
        return HttpResponse.ok();
    }
}

client/src/main/java/client/DemoClient.java

package client;

import io.micronaut.http.HttpResponse;
import io.micronaut.http.annotation.Patch;
import io.micronaut.http.client.annotation.Client;

@Client("http://localhost:8080")
public interface DemoClient {

    @Patch("/demo")
    HttpResponse callServer();
}

client/src/main/java/client/ClientCommand.java

package client;

import io.micronaut.configuration.picocli.PicocliRunner;
import io.micronaut.context.ApplicationContext;

import jakarta.inject.Inject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

@Command(name = "client", description = "...",
        mixinStandardHelpOptions = true)
public class ClientCommand implements Runnable {

    static final Logger log = LoggerFactory.getLogger(ClientCommand.class);

    @Inject
    DemoClient client;

    public static void main(String[] args) throws Exception {
        PicocliRunner.run(ClientCommand.class, args);
    }

    public void run() {
        log.info("Before calling server.");
        client.callServer();
        log.info("After calling server");
    }
}

如果您运行服务器,那么您可以运行客户端并看到一个成功的往返行程,但主体为空。

~ $ git clone git@github.com:jeffbrown/patchwithemptyresponse.git
Cloning into 'patchwithemptyresponse'...
remote: Enumerating objects: 51, done.
remote: Counting objects: 100% (51/51), done.
remote: Compressing objects: 100% (31/31), done.
remote: Total 51 (delta 4), reused 51 (delta 4), pack-reused 0
Receiving objects: 100% (51/51), 65.64 KiB | 1.04 MiB/s, done.
Resolving deltas: 100% (4/4), done.

运行服务器:

~ $ cd patchwithemptyresponse 
patchwithemptyresponse (main)$ ./gradlew server:run

> Task :server:compileJava
Note: Creating bean classes for 1 type elements

> Task :server:run
 __  __ _                                  _   
|  \/  (_) ___ _ __ ___  _ __   __ _ _   _| |_ 
| |\/| | |/ __| '__/ _ \| '_ \ / _` | | | | __|
| |  | | | (__| | | (_) | | | | (_| | |_| | |_ 
|_|  |_|_|\___|_|  \___/|_| |_|\__,_|\__,_|\__|
  Micronaut (v3.1.0)

18:17:56.201 [main] INFO  io.micronaut.runtime.Micronaut - Startup completed in 748ms. Server Running: http://localhost:8080

运行客户端:

~ $ cd patchwithemptyresponse 
patchwithemptyresponse (main)$ 
patchwithemptyresponse (main)$ ./gradlew client:run
Starting a Gradle Daemon, 1 busy and 1 incompatible and 1 stopped Daemons could not be reused, use --status for details

> Task :client:compileJava
Note: Creating bean classes for 2 type elements

> Task :client:run
18:18:34.618 [main] INFO  i.m.context.env.DefaultEnvironment - Established active environments: [cli]
18:18:34.908 [main] INFO  client.ClientCommand - Before calling server.
18:18:35.680 [main] INFO  client.ClientCommand - After calling server

【讨论】:

  • @shmish111 - 答案中链接的项目是否演示了如何做你想做的事情?
猜你喜欢
  • 2021-09-14
  • 1970-01-01
  • 1970-01-01
  • 2023-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多