ilGiudice ilGiudice - 25 days ago 6
AngularJS Question

Passing Object to Java from AngularJS

i'm in trouble with this problem.
I've a Javascript structur like this:

$scope.setting= {
enabled: false,
host:"",
port:"",
user:"",
pwd:"",
path:"/",
filePrefix:"",
type:"",
sendInterval:"",
dataPeriod:"",
compression:false,
subscription:[]
};


In the controller i modify the subscription array, but when i pass it to the java code:

$http.post('/api/testAndSetFTPSetting', $scope.setting)
.success(function (data) {
console.log(data);
})
.error(function (data, status, header, config) {
});


the subscription array is null.

Here the API

@RequestMapping(value = {"/api/testAndSetFTPSetting"}, method={RequestMethod.POST})
@ResponseBody
public boolean testAndSetFTPSetting(FTPConfiguration ftp) throws JAXBException {

System.out.println(ftp.getSubscribtion().size()); // here i've ever 0 and ftp.getSubscribtion() return me null

return true;

}


Here the Java Class who controls the object:

@XmlRootElement(name="FTPconfiguration")
@XmlAccessorType (XmlAccessType.FIELD)
public class FTPConfiguration{
boolean enabled = false;
String host="127.0.0.1";
int port=22;
String user="root";
String pwd="";
String path="/";
String filePrefix="data";
FTPType type=FTPType.SFTP;
int sendInterval=15;
int dataPeriod=5;
boolean compression=false;
@XmlElementWrapper(name="subscriptions")
List<String> subscription = new LinkedList<String>();


public FTPConfiguration() {
}

public FTPConfiguration(boolean enabled,String host, int port, String user, String pwd, String path, String filePrefix,
FTPType type, int sendInterval, int dataPeriod, boolean compression, List<String> subscription) {
super();
this.host = host;
this.port = port;
this.user = user;
this.pwd = pwd;
this.path = path;
this.filePrefix = filePrefix;
this.type = type;
this.sendInterval = sendInterval;
this.dataPeriod = dataPeriod;
this.compression = compression;
if(subscription != null)
this.subscription.addAll(subscription);
}
// setter and getter method


Where is my fault?

Answer

Finally solved!!! The problem was that the javascript array in java is a comma separated String. For this the value that i received was null!

EX 
JavaScript OBJ
var arr = ["1", "2", "3"];
$http.post('someUrl', arr)...

Java
@RequestMapping(value = {"/someUrl"}, method={RequestMethod.POST})
public void foo(String s) { // s will be = "1,2,3"

}
Comments