我正在向PATCH
服务器发送一个请求,它给了我一个 200 并且没有正文(完全可以接受)但是我不知道如何配置 micronaut@Patch
注释来处理这个问题。无论我尝试什么,它都会尝试将空响应解码为 JSON(至少我认为这就是它正在做的事情,因为它会给出意外错误,而不是当正文错误时出现的正常杰克逊错误)。我在 micronaut 文档中找不到有关如何执行此操作的任何内容。
问问题
203 次
1 回答
0
我正在向服务器发送一个 PATCH 请求,它给了我一个 200 并且没有正文(完全可以接受)但是我不知道如何配置 micronaut @Patch 注释来处理这个
我认为这将@Patch
与其他动词一样起作用。
请参阅https://github.com/jeffbrown/patchwithemptyresponse上的项目。
服务器/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();
}
}
客户端/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();
}
客户端/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
于 2021-11-04T23:21:41.007 回答