The HttpServer created by Java returns json data

the environmental background of the problems and what methods you have tried

in my implementation of the HttpHandler, rewrite handle method,
writes a normal string (such as "hello") to responseBody, and the browser accesses this method to get the corresponding data.
but write the json string (for example: "{" pid ":" 510229197206267348 "," pname ":" Zhang San "}"). The browser accesses Status Code: 200OK, but Status is failed.

.

I guess it"s the data format type set in the background header, but headers.set ("Content-Type", "application/json; charset=utf-8") is set; it doesn"t work either.

write "{" pid ":" 510229197206267348 "," pname ":" Zhang San "}" access service method screenshot:

"hello word":

related codes

public static void main(String[] arg) throws Exception {
        HttpServer server = HttpServer.create(new InetSocketAddress(8001), 0);    
        server.createContext("/post", new BasicPostHttpHandler());        
        //  excutor
        server.setExecutor(null);
        server.start();
    }

public class BasicPostHttpHandler implements HttpHandler{

    @Override
    public void handle(HttpExchange httpExchange) throws IOException {
        
        InputStream is = httpExchange.getRequestBody();
        
        String requestData = is2string(is);
        System.out.println("request: " + requestData);         
        String response = "{\"pid\":\"510229197206267348\",\"pname\":\"\"}";   // failed
       // String response = "hello world";  //  
        System.out.println("response: " + response);        
        is.close();
              
        Headers headers = httpExchange.getResponseHeaders();        
        headers.set("Content-Type", "application/json; charset=utf-8"); 
        headers.set("Access-Control-Allow-Origin", "*");
        headers.set("Access-Control-Allow-Methods","GET,POST,PUT,DELETE,OPTIONS");
        headers.set("Access-Control-Allow-Headers", "Origin,X-Requested-With,Content-Type,Accept"); 
               
        httpExchange.sendResponseHeaders(200, response.length());       
        OutputStream os = httpExchange.getResponseBody();
        os.write(response.getBytes());
        os.close();       
    }
    
    private String is2string(InputStream is) throws IOException {
        final int bufferSize = 1024;
        final char[] buffer = new char[bufferSize];
        final StringBuilder out = new StringBuilder();
        Reader in = new InputStreamReader(is, "UTF-8");
        
        for (; ; ) {
            int rsz = in.read(buffer, 0, buffer.length);
            if (rsz < 0)
                break;
            out.append(buffer, 0, rsz);
        }
        return out.toString();
    }

}

what result do you expect? What is the error message actually seen?

May.12,2021

@ half of the summer I have a little bit of research here, you can take a look at my this-> java parse http , it can solve your problem


has nothing to do with json! Yours is a Chinese problem!


representation of Chinese bytes,

httpExchange.sendResponseHeaders(200, response.length());  
httpExchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, response.getBytes().length);
Menu