JSON数组不会在Jersey中映射

时间:2016-04-10 03:06:20

标签: java c# json jersey

我正在使用Maven项目构建Jersey服务器,除非我尝试从C#程序发送请求,否则我的一切工作正常我已经知道用户将与JSON显示为的数组进行交互:

[{"Weight":0.0,"RFIDCode":0},{"Weight":55.5,"RFIDCode":1}]

当我将代码发送到服务器时,我最终得到一个空的Java数组 我的C#代码在

之下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.IO;
using RestSharp;

namespace POSTJSONList
{
    class Program
    {
        static void Main(string[] args)
        {
            List<ExampleMen> people = new List<ExampleMen>();
            JsonSerializer ser = new JsonSerializer();
            for (int i = 0; i < 5; i++)
            {
                ExampleMen person1 = new ExampleMen(i, i * 55.5);
                people.Add(person1);
            }
            string json = JsonConvert.SerializeObject(people);
            Console.WriteLine(json);
            Console.ReadLine();
            var client = new RestClient("http://localhost:9998");
            var request = new RestRequest("/trash", Method.POST);
            request.AddJsonBody(json);
            client.ExecuteAsync(request, response => {
                Console.WriteLine(response.Content);
            });
            Console.ReadLine();
        }
    }
}

我的Java代码在下面

@POST
    @Produces("application/json")
    @Consumes(MediaType.APPLICATION_JSON)
    //@Param recycleList a list of all of the data that we have collected
    public String createtrash_records(trashpickup_type trashList[]){
        System.out.println("Im Here Guys!!!!");
        // TODO update this to take the information we pass through to it and create individual objects based on it. 
        // TODO modify to return success code
        billing billSystem = billing.getInstance();
        systemTotalsTrash sysTotalsTrashSystem = systemTotalsTrash.getInstance();
        int i = 1;
        for(trashpickup_type alpha : trashList)
        {   
            System.out.println(alpha.getRFIDCode() + alpha.getWeight());
            i++;
            /*try {
                billSystem.updateBillingRecord(rec.getUserName(), 0, rec.getWeight());
                sysTotalsTrashSystem.updateSystemTotals(rec.getWeight());
                getDao().createIfNotExists(rec);

            } catch (SQLException e) {
                e.printStackTrace();
            }
            */
        }
        return "It worked";
    }

模型代码(JSON映射到的代码)是Here

package com.gallup.gethip.model;

import java.util.Date;

import javax.xml.bind.annotation.XmlRootElement;

import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;

@XmlRootElement
@DatabaseTable(tableName="trash_records")
public class trashpickup_type {
    @DatabaseField(columnName = "RFIDCode")
    private int RFIDCode;
    @DatabaseField(columnName = "Weight")
    private double Weight;
    @DatabaseField(columnName = "PickedUp")
    private Date PickedUp;
    public trashpickup_type()
    {

    }
    public void setRFIDCode(int RFIDCode)
    {
        this.RFIDCode = RFIDCode;
    }
    public void setWeight(double Weight)
    {
        this.Weight = Weight;
    }
    public void setPickedUP(Date PickedUp)
    {
        this.PickedUp = PickedUp;
    }

    public double getWeight()
    {
        return Weight;
    }
    public double getRFIDCode()
    {
        return RFIDCode;
    }
    public Date getPickedUp()
    {
        return PickedUp;
    }
}

任何建议表示赞赏。如有需要,我会根据要求发布更多信息。

1 个答案:

答案 0 :(得分:1)

您将people列表序列化为json,然后使用request.AddJsonBody将其添加到请求中。但AddJsonBody将再次将参数序列化为json,因此最终得到一个双序列化值 - 在服务器端 - 无法解析为trashList[]。因此,删除客户端序列化代码,然后编写

request.AddJsonBody(people);