gpt4 book ai didi

java - 如何序列化请求?

转载 作者:行者123 更新时间:2023-12-02 11:42:58 26 4
gpt4 key购买 nike

我有以下请求,如果服务发生错误,我想将请求序列化保存在数据库中,以便在其他时刻再次发送。

        URI uri = fromUri(config.getUri()).path("/myservice").build();
Client client = ClientProducer.get();

response = client
.target(uri)
.request()
.headers(obterCabecalhos())
.accept(MediaType.APPLICATION_JSON)
.post(Entity.json(myEntity));

if (response.getStatus() != OK.getStatusCode()) {
throw new TSEIntegracaoException();
// Here I'd like to serialize the request and save in a database
}

最佳答案

我认为这是不可能的。如何创建一个类来封装您的请求数据(uri、 header 、有效负载)并序列化该类的实例。

一个例子:

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.URI;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedHashMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.xml.bind.DatatypeConverter;

public class DemoClient {

public static void main(String[] args) throws Exception {
Client client = ClientBuilder.newClient();

URI uri = URI.create("http://localhost:8000");
Map<String, List<Object>> headers = new HashMap<>();
headers.put(HttpHeaders.ACCEPT, Arrays.asList((Object) MediaType.APPLICATION_JSON));

PostRequestData requestData = new PostRequestData(uri, headers, new String("hello world"));

Response response = requestData.post(client);
if (response.getStatus() != Status.ACCEPTED.getStatusCode()) {

// let's serialise it into binary
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteStream);
objectOutputStream.writeObject(requestData);
// save bytestream or print it?
String bin = DatatypeConverter.printHexBinary(byteStream.toByteArray());
System.out.println(bin);

// let's replay the request
byte[] newBytes = DatatypeConverter.parseHexBinary(bin);
ObjectInputStream oin = new ObjectInputStream(new ByteArrayInputStream(newBytes));
PostRequestData newRequestdata = (PostRequestData) oin.readObject();

Response newResponse = newRequestdata.post(client);

if (newResponse.getStatus() != Status.ACCEPTED.getStatusCode()) {
System.out.println("give up!");
}
}
}

static class PostRequestData implements Serializable {

private static final long serialVersionUID = -5786067257552259115L;

final URI uri;

final Map<String, List<Object>> headers;

final Serializable entity;

public PostRequestData(URI uri, Map<String, List<Object>> headers, Serializable entity) {
this.uri = uri;
this.headers = headers;
this.entity = entity;
}

public Response post(Client client) {
MultivaluedHashMap<String, Object> multimap = new MultivaluedHashMap<String, Object>();
headers.forEach((k,v) -> multimap.put(k, v));

return client.target(uri)
.request()
.headers(multimap)
.post(Entity.text(entity));
}

}

}

关于java - 如何序列化请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48401872/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com