简体   繁体   中英

BufferedReader + BufferedWriter RESTful web service in wildfly 19

I have a piece of code

@RequestScoped
@Path("test")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_XML)
public class Test {
    private static final Logger LOG = Logger.getLogger(Test.class.toString());

    @POST
    public Response createRequest(InputStream request) throws IOException {
        char[] buff = new char[1024];
        int in = 0;
        BufferedReader reader = new BufferedReader(new InputStreamReader(request));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out, "UTF-8"));
        try {
            while ((in = reader.read(buff)) != -1) {
                //writer.write(buff);
                writer.write(buff, 0, in);

            }
        } finally {
            writer.flush();
            writer.close();
            reader.close();
        }
        return Response.ok().build();
    }
}

The problem is that this service writes to the output incomplete xml, ie

Input

<note>
   <to>Tove</to>
   <from>Jani</from>
   <heading>Reminder</heading>
   <body>Don't forget me this weekend!</body>
</note>

output

04:55:44,190 INFO  [stdout] (default task-1) <note>
04:55:44,191 INFO  [stdout] (default task-1) <to>Tove</to>
04:55:44,191 INFO  [stdout] (default task-1) <from>Jani</from>
04:55:44,198 INFO  [stdout] (default task-1) <heading>Reminder</heading>
04:55:44,198 INFO  [stdout] (default task-1) <body>Don't forget me this weekend!</body>

a missing

</note>

... Why?

and when I try a second time to send a request, console is empty and I need to restart wildfly to get incomplete xml into output... Why?

NOTE:

I get no errors to console

Okay after long session of failures, I found why it was not working... And it is kind of disturbing...

So... the working example

@RequestScoped
@Path("test")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_XML)
public class ConnectorResourceV1 {
    private static final Logger LOG = Logger.getLogger(ConnectorResourceV1.class.toString());

    @POST
    public Response test(InputStream request) throws IOException {
        char[] buff = new char[16 * 1024];
        int in = 0;
        int lastIn = 0;
        BufferedReader read = new BufferedReader(new InputStreamReader(request));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
    while ((in = read.read(buff)) != -1) {
        writer.write(buff, lastIn, in);
        if (in < buff.length) {
            if (in == 0) {
                lastIn = in;
            } else {
                lastIn = lastIn + in;
            }
        }
    }
        writer.write("\r\n");
        writer.flush();
        return Response.ok().build();
    }
}

PAY attention to the writer.write("\\r\\n"); it has to be here and it seams that it is a stream terminating expression??

ALSO, because I'm using System.out as my output I don't need to close it otherwise you won't see an output in your console session.

A very disturbing experience...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM