I need to send a Java object from a client to a Spring controller. I have tried the following. But does not work.
My bean class - I have the same package and class both in the client and in the service
public class DataObj implements Serializable {
private String stringData;
private byte[] byteData;
public String getStringData() {
return stringData;
}
public void setStringData(String stringData) {
this.stringData = stringData;
}
public byte[] getByteData() {
return byteData;
}
public void setByteData(byte[] byteData) {
this.byteData = byteData;
}
}
My controller
@RequestMapping(value = "/an/data", method = RequestMethod.POST)
public void subscribeUser(@RequestBody DataObj subscription){
System.out.println("DD");
bytes = subscription.getByteData();
}
My client is Apache
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httppost = new HttpPost("http://localhost:8080/contex/an/data");
httppost.setEntity(new SerializableEntity((Serializable) dataObj , false));
httpClient.execute(httppost);
My client is URLConnection
URL url = new URL("http://localhost:8080/contex/an/data");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
ObjectOutputStream writer = new ObjectOutputStream (conn.getOutputStream());
writer.writeObject(dataObj );
writer.flush();
conn.connect();
writer.close();
System.out.println(conn.getResponseCode());
Both versions do not work. The controller is trying to redirect to the access denied page. Correct me if my understanding is wrong, forgive me if this is repeated. The JSON wrapper won't help me as the java object has a byte array. Therefore, note that.
UPDATE
I get the following log
org.apache.tomcat.util.http.Parameters processParameters
INFO: Character decoding failed. Parameter...... [Showing my bean class package and the data in non readable format]
source
share