休息 - json响应有一个未知领域

时间:2016-11-10 01:24:38

标签: java json hibernate rest

我使用不同的资源设置了一个rest webservice。

其中一个是可以通过/ api / customers

访问的客户

客户类有id,地址,名称和电子邮件字段但由于某种原因,json响应如下所示:

{
"type": "customer",
"address": {
  "city": "Kottes-Purk",
  "country": "Pakistan",
  "street": "Julius-Raab-Straße 008"
},
"email": "hana.lammert@hotmail.com",
"name": "Prof. Dr. David Mikitenko"
}

我不知道为什么json响应中有“type”字段。有人可以解释为什么会这样吗?

客户类看起来像这样:

@XmlRootElement
@Entity
@Table(name = "customers")
public class Customer extends Model {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;

@Column(name = "name")
private String name;

@Column(name = "email")
private String email;

@ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
@JoinColumn(name = "address_id")
private Address address;

@OneToMany(fetch = FetchType.LAZY, mappedBy = "customer")
private List<Booking> bookings;

public Customer() {}

public Customer(String email, Address address, String name) {
    this.email = email;
    this.address = address;
    this.name = name;
}



public int setId() {
    return id;
}

public int getId() {
    return id;
}


public String getName() {
    return name;
}


public void setName(String name) {
    this.name = name;
}


public String getEmail() {
    return email;
}


public void setEmail(String email) {
    this.email = email;
}


public Address getAddress() {
    return address;
}


public void setAddress(Address address) {
    this.address = address;
}


@XmlTransient
public List<Booking> getBookings() {
    return bookings;
}

public void setBookings(List<Booking> bookings) {
    this.bookings = bookings;
}


public static List<Customer> all() {
    return (List<Customer>) Database.all(Customer.class);
}


public static Customer find(int id) {
    return (Customer) Database.find(Customer.class, id);
}


public static boolean exists(int id) {
    return Database.exists(Customer.class, id);
}


public static List<Customer> where(String column, String value) {
    return (List<Customer>) Database.where(Customer.class, column, value);
}

}

在CustomerResource类中:

@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Customer> getCustomers(){
    return agencyManager.getCustomers();
}

我在这里缺少什么?谢谢

模特课:

public abstract class Model implements Serializable {


public void save() {
    EntityManager entityManager = HibernateUtil.getEntityManagerFactory().createEntityManager();
    entityManager.getTransaction().begin();
    entityManager.persist(this);
    entityManager.getTransaction().commit();
    entityManager.close();
}


public void delete() {
    EntityManager entityManager = HibernateUtil.getEntityManagerFactory().createEntityManager();
    entityManager.getTransaction().begin();
    entityManager.remove(entityManager.contains(this) ? this : entityManager.merge(this));
    entityManager.getTransaction().commit();
    entityManager.close();
}
}

1 个答案:

答案 0 :(得分:1)

由于您的ID设置器ID丢失,因此应该

using System;

namespace BitConverterTest
{
    class Program
    {
        const int iters = 1024 * 1024 * 1024;
        const int arrayLen = iters / 4;
        static byte[] array = new byte[arrayLen];

        static void Main(string[] args)
        {
            //test1(1, 2, 3, 4);
            //test2(1, 2, 3, 4);
            test3(1, 2, 3, 4);
            //test4(1, 2, 3, 4);
            test5(1, 2, 3, 4);
            test6(1, 2, 3, 4);

            // Fill array with good PRNG data
            var rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
            rng.GetBytes(array);

            test7();
            test8();
            test9();
        }

        // BitConverter with aligned input
        static void test3(byte w, byte x, byte y, byte z)
        {
            int res = 0;
            var timer = System.Diagnostics.Stopwatch.StartNew();
            var b = new byte[] { w, x, y, z };
            for (int i = 0; i < iters; i++)
                res = BitConverter.ToInt32(b, 0);
            Console.WriteLine("test3: " + timer.Elapsed + " " + res);
        }

        // Inline bitfiddling with separate variables.
        static void test5(byte w, byte x, byte y, byte z)
        {
            long res = 0;
            var timer = System.Diagnostics.Stopwatch.StartNew();
            var b = new byte[] { w, x, y, z };
            for (int i = 0; i < iters; i++)
            {
                int a = w | (x << 8) | (y << 16) | (z << 24);
                res += a;
            }
            Console.WriteLine("test5: " + timer.Elapsed + " " + res);
        }

        // Inline bitfiddling with array elements.
        static void test6(byte w, byte x, byte y, byte z)
        {
            long res = 0;
            var timer = System.Diagnostics.Stopwatch.StartNew();
            var b = new byte[] { w, x, y, z };
            for (int i = 0; i < iters; i++)
            {
                int a = b[0] | (b[1] << 8) | (b[2] << 16) | (b[3] << 24);
                res += a;
            }
            Console.WriteLine("test6: " + timer.Elapsed + " " + res);
        }

        // BitConvert from large array...
        static void test7()
        {
            var its = iters / arrayLen * 4; // *4 to remove arrayLen/4 factor.
            var timer = System.Diagnostics.Stopwatch.StartNew();
            long res = 0;
            for (var outer = 0; outer < its; outer++)
            {
                for (var pos = 0; pos < arrayLen; pos += 4)
                {
                    var x = BitConverter.ToInt32(array, pos);
                    res += x;
                }
            }
            Console.WriteLine("test7: " + timer.Elapsed + " " + res);
        }

        // Bitfiddle from large array...
        static void test8()
        {
            var its = iters / arrayLen * 4;
            var timer = System.Diagnostics.Stopwatch.StartNew();
            long res = 0;
            for (var outer = 0; outer < its; outer++)
            {
                for (var pos = 0; pos < arrayLen; pos += 4)
                {
                    int x = array[pos] | (array[pos + 1] << 8) | (array[pos + 2] << 16) | (array[pos + 3] << 24);
                    res += x;
                }
            }
            Console.WriteLine("test8: " + timer.Elapsed + " " + res);
        }

        // unsafe memory operations from large array...
        // (essentialy internals of BitConverter without param checks, etc)
        static unsafe void test9()
        {
            var its = iters / arrayLen * 4;
            var timer = System.Diagnostics.Stopwatch.StartNew();
            long res = 0;
            int value = 0;
            for (var outer = 0; outer < its; outer++)
            {
                for (var pos = 0; pos < arrayLen; pos += 4)
                {
                    fixed (byte* numPtr = &array[pos])
                    {
                        value = *(int*)numPtr;
                    }
                    int x = *(int*)&value;
                    res += x;
                }
            }
            Console.WriteLine("test9: " + timer.Elapsed + " " + res);
        }

    }
}

类型'“type”:“customer”,'可能会从Model类添加到JSON。检查Model类