To post JSON data to the server using Java, you need to use the HTTP POST method and set the correct MIME type for the request body. The correct MIME type for JSON is application/json. In this Java POST JSON example, the Content-Type: application/json request header specifies the media type for the resource in the body, and the Accept: application/json header tells the server that the client is expecting JSON. The Java code was automatically generated for the POST JSON example.
What MIME type should I use when posting JSON data to the server?
How to request JSON data from the server?
If your Java client expects a JSON from the server, it must also send the Accept: application/json request header. If you don't send the Accept header, the server may return data in a different format. For example, if the server can handle both JSON and XML requests on the same API endpoint, setting the Accept request header to application/json will let the server know that the Java client is expecting JSON and provide the data in that format rather than in XML.
POST JSON Example
Example of sending JSON data to ReqBin echo URL (click on Generate Code to see the Java code).