Erstens, ich brauche Rest Service zum Senden einer POJO-Klasse include Byte-Array-Feld für Bild und andere POJO-Klasse. Außerdem müssen Sie diesen Service mit dem Trikot-Client nutzen. Es ist möglich, diese mit Application/Octet-Stream MediaType zu erreichen. Ich habe es schon für nur Bilddatei getan und es funktioniert. Was ist der richtige Weg?Wie produziert und konsumiert man den Restservice mit Hilfe von Trikot über application/octet_stream?
public class Sample{
int sampleId;
Byte[] image;
Foo foo;
//constructor
//getter setter
}
public class GetDataImage {
@GET
@Path("/gets")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response getFile(@QueryParam("id") String id) throws IOException {
File file = new
File("..\test_image.jpg");
RenderedImage image2 = ImageIO.read(file);
Foo foo = new Foo();
Sample sample = new Sample (1, new Byte[] {},foo );
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectMapper mapper = new ObjectMapper(new BsonFactory());
mapper.writeValue(baos, responseChipoutImage);
StreamingOutput stream = new StreamingOutput() {
@Override
public void write(OutputStream output) throws IOException {
try {
// ImageIO.write(image2, "jpg", output);
new ObjectOutputStream(output).writeObject(responseChipoutImage);
} catch (Exception e) {
e.printStackTrace();
}
}
};
return Response.ok(stream, "application/octet-stream")
.header("content-disposition", "attachment; filename = " + image2.toString())
.build();
}
}
Dies ist der Kunde:
public class Client {
private static final String BASE_URI = "http://localhost:8090/Test/gets";
public Client() throws IOException {
try {
Client client = Client.create();
WebResource objWebResource = client.resource(BASE_URI);
ClientResponse response = objWebResource.path("/").queryParam("id", "1")
.type(javax.ws.rs.core.MediaType.APPLICATION_OCTET_STREAM).get(ClientResponse.class);
System.out.println("response : " + response);
if (response.getStatus() == Status.OK.getStatusCode() && response.hasEntity()) {
ResponseSample responseSample = response.getEntity(ResponseSample.class);
// InputStream input = (InputStream)response.getEntity(InputStream.class);
// BufferedImage bf = ImageIO.read(input);
// File outputfile = new File("../test.jpeg");
// ImageIO.write(bf, "jpg", outputfile);
ObjectMapper mapper = new ObjectMapper(new BsonFactory());
// deserialize data
}
} catch (UniformInterfaceException e) {
e.printStackTrace();
} catch (ClientHandlerException e) {
e.printStackTrace();
}
}
public static void main(String... args) throws IOException {
new Client();
}