0

我的问题:

我已经用restkit 0.20在我的iphone应用程序中构建了一个rest客户端。我的 RESTService 是用 jax-rs 构建的,包括 jackson。

有一个名为“客户”的类,我保存我的客户数据。

@XmlRootElement                                
public class Customer extends BaseEntity {

private int customer_id;
private String appell;
private String firstname;
private String lastname;
private Date birthday;
private String street;
private String streetnr;
private String zipcode;
private String place;

public int getCustomer_id() {
    return customer_id;
}

public void setCustomer_id(int customer_id) {
    this.customer_id = customer_id;
}

public String getAppell() {
    return appell;
}

public void setAppell(String appell) {
    this.appell = appell;
}

public String getFirstname() {
    return firstname;
}

public void setFirstname(String firstname) {
    this.firstname = firstname;
}

public String getLastname() {
    return lastname;
}

public void setLastname(String lastname) {
    this.lastname = lastname;
}

public Date getBirthday() {
    return birthday;
}

public void setBirthday(Date birthday) {
    this.birthday = birthday;
}

public String getStreet() {
    return street;
}
public void setStreet(String street) {
    this.street = street;
}
public String getStreetnr() {
    return streetnr;
}
public void setStreetnr(String streetnr) {
    this.streetnr = streetnr;
}
public String getZipcode() {
    return zipcode;
}
public void setZipcode(String zipcode) {
    this.zipcode = zipcode;
}
public String getPlace() {
    return place;
}
public void setPlace(String place) {
    this.place = place;
}

我能做些什么?为什么在 RestKit 中总是记录此错误:

Expected status code in (200-299), got 400" UserInfo=0x956e2f0 {NSLocalizedRecoverySuggestion=Unrecognized field "customer" (Class customer.model.Customer), not marked as ignorable

我没有名为客户的字段。

这是我的 POST 方法:

@POST
@Path("/insert")
@Consumes(MediaType.APPLICATION_JSON)
public Response insertCustomer(Customer customer) {
    String result = "Customer inserted " + customer.getLastname();
    customer.createEntity();
    return Response.status(201).type(MediaType.APPLICATION_JSON).entity(result).build();
}

这是在我的 ios 应用程序中调用 @GET 方法时生成的 JSON 对象 (Jackson) 的示例。

 {
"appell": "Frau",
"birthday": "1991-10-01T00:00:00+01:00",
"customer_id": "3",
"firstname": "Max",
"lastname": "Mustermann",
"place": "München",
"street": "Einsteinweg",
"streetnr": "19",
"zipcode": "81858"

}

这是 JSON 对象的一个​​示例,当我的 ios 应用程序调用显示的 @POST 方法时:

{"customer":{"streetnr":"19",
 "zipcode":"81829",
 "customer_id":0,
 "place":"München",
 "appell":"Herr",
 "firstname":"Heinrich",
 "birthday":"1991-10-01T00:00:00+01:00",
 "lastname":"Maier",
 "street":"Lehrer-Wirth-Str.19"}}

我是使用 restkit 和 jax-rs 的绝对新手。

我刚刚添加了一个 @GET 方法来请求 xml 格式。在这里你可以看到它:

@GET
@Path("/listallcustomers")
@Produces(MediaType.APPLICATION_XML)
public ArrayList<Customer> showAllCustomers() {
    ArrayList<Customer> list = DBAccess.getInstance().getAllCustomers();
    return list;
}

这是结果:

<customers>
  <customer>
  <appell>Frau</appell>
  <birthday>1991-10-01T00:00:00+01:00</birthday>
  <customer_id>3</customer_id>
  <firstname>Max</firstname>
  <lastname>MUstermann</lastname>
  <place>München</place>
  <street>Einsteinstr.</street>
  <streetnr>19</streetnr>
  <zipcode>82838</zipcode>
</customer>
  ...

我创建 JSON 对象的 @GET 方法如下:

@GET
@Path("/listall")
@Produces(MediaType.APPLICATION_JSON)
public ArrayList<Customer> showAllJsonCustomers() {
    ArrayList<Customer> list = DBAccess.getInstance().getAllCustomers();
    return list;
}

这是它创建的 JSON 对象的示例:

{
"lastname": "Müller",
"customer_id": 6,
"appell": "Herr",
"firstname": "pupsi",
"birthday": "1986-10-16",
"street": "Lehrer-Wirth-Str",
"streetnr": "19",
"zipcode": "81829",
"place": "München"
},
...
4

0 回答 0