从AngularJS将对象传递给Java

时间:2016-11-03 15:10:09

标签: javascript java angularjs

我遇到了这个问题。 我是这样的Javascript结构:

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

在控制器中我修改了订阅数组,但当我将它传递给java代码时:

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

订阅数组为空。

这里是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;

    }

这里是控制对象的Java类:

    @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

我的错在哪里?

1 个答案:

答案 0 :(得分:0)

终于解决了!问题是java中的javascript数组是逗号分隔的String。为此,我收到的值为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"

}