1

I am trying to introduce a JSON String into H2 Data Base. It is a Spring based API-REST.

Here is the Java code used to call the embedded DataBase:

URL url = new URL("http://127.0.0.1:8080/messages/");
System.out.println("URL: " + url.toString());
HttpURLConnection conexion = (HttpURLConnection) url.openConnection();

conexion.setDoOutput(true);
conexion.setDoInput(true);
conexion.setRequestMethod("POST");
conexion.setRequestProperty("Accept", "application/json");
conexion.setRequestProperty("Content-Type", "application/json; charset=UTF-8");

OutputStreamWriter wr = new OutputStreamWriter(conexion.getOutputStream(), "UTF-8");
String mensaje = message.convertToJson();
System.out.println("Mensaje: " + mensaje);

wr.write(mensaje); 
wr.flush();
wr.close();

For some reason wr.write(mensaje) does not do what it is suppose to do. The execution does not return any error nor exception, everything seems to work fine.

JSon String I want to insert

DataBase content

Note that the Json showed in the DataBase content image was inserted using Postman. But after running the application and the API-Rest the other Json string was not included.

Thank you, Héctor

hectorhfm
  • 105
  • 9
  • GET is working as it returns the content placed in the database [link](http://imgur.com/c1xHGLq) – hectorhfm May 23 '16 at 18:12
  • Try to call https://docs.oracle.com/javase/7/docs/api/java/net/HttpURLConnection.html#setFixedLengthStreamingMode(int) with mensaje.getBytes("UTF-8").length as contentLength – Nicolas Filotto May 23 '16 at 18:20
  • Thank you for your answer here it is explained with more precision: http://stackoverflow.com/questions/20020902/android-httpurlconnection-how-to-set-post-data-in-http-body This way works! – hectorhfm May 23 '16 at 20:04

0 Answers0