3

I'm a new member in Restful API, I'm writing a GET method:

@RequestMapping(method = RequestMethod.GET, value = "/resourcerecords", produces={"application/json", "application/xml"})
public @ResponseBody Object getRRs(@RequestBody RRRequest requestBody){
   // do something
}

The RRRequest class:

public class RRRequest{
   private RRREC reqObject;
   // getter and setter
}

The RRREC class:

public class RRREC{
   protected String infraAddr;
   protected RRINFRATYPE infraType;
   // getter and setter
}   

And the RRINFRATYPE class:

public enum RRINFRATYPE {
    V_6_ADDRESS("V6ADDRESS"),
    OBJECT("OBJECT"),
    ZONE("ZONE"),
    V_4_REVERSEZONE("V4REVERSEZONE"),
    V_6_REVERSEZONE("V6REVERSEZONE"),
    NODE("NODE"),
    ALL("ALL");
    private final String value;

    RRINFRATYPE(String v) {
       value = v;
   }

   public String value() {
      return value;
   }

   public static RRINFRATYPE fromValue(String v) {
     for (RRINFRATYPE c: RRINFRATYPE.values()) {
        if (c.value.equals(v)) {
            return c;
        }
    }
    throw new IllegalArgumentException(v);
    }
}

Then, I sent a request GET with RequestBody ( I use Fiddler Web Debugger)

"reqObject" : {    
   "infraAddr" : "192.168.88.4",
   "infraType": {
       "value": "OBJECT"
   }
}

I get 400 Bad Request. If I change to

 "reqObject" : {    
   "infraAddr" : "192.168.88.4",
   "InfraType": {
       "value": "OBJECT"
   }
}

I can debug.

However, The reqObject only receive infraAddr with "192.168.88.4", the InfraType is null.

Who can explain to me, why I must be use "InfraType" instead of "infraType" and how to send value for InfraType.

Tam Nguyen
  • 93
  • 1
  • 3
  • 11

1 Answers1

0

The first one is when your api in GET method you still cant send body of request to server, try to change it to POST.

Because you use ENUM in your Object so you should define a converter like Converting JSON to Enum type with @RequestBody

But in this case, I think the fastest way is change infraType to String and use switch case with String on server side.

public class RRREC{
   protected String infraAddr;
   protected String infraType;
   // getter and setter
}   

Your JSON will be:

{
    "reqObject" : {    
        "infraAddr" : "192.168.88.4",
        "infraType": "OBJECT"
    }
}
Community
  • 1
  • 1
codeaholicguy
  • 1,671
  • 1
  • 11
  • 18