Java RAML parser - How can I access response code and example response?


#1

Hi,

I have a need to access the response code and example response, as defined in a .raml file, from my Java code. Is there a way of achieving this?

I’ve managed to get each Resource, and all of their ‘nested’ resources, and for each Resource I’ve managed to get their Actions as well. But I can’t seem to access elements of the Actions’ Response members. Only body, description and headers are available.

Am I missing something?


#2

Could you post your RAML here. Let me test this.

The Action class does have a method to get the responses as you can see here: https://github.com/raml-org/raml-java-parser/blob/master/src/main/java/org/raml/model/Action.java#L133


#3

I am using that exact method, but the Response objects that are returned do not have the members to represent the response code or the example response.

RAML:

/{product_uuid}:
    get:
      responses:
        200:
          body:
            application/json:
              example: |
                {
                  "data" : {
                    "uuid" : "0021b6c3-d838-4d78-b59b-0ea42133e177",
                    "product_name" : "RECKITT BENCKISER",
                    "product_categories_uuid" : "de66f565-18bf-11e4-8694-00163e2ce2b8"
                  },
                  "success" : true,
                  "status" : 200
                }

#4

I’ve found the missing members that I was looking for. Actually I was just looking in the wrong place. The Objects are nested exactly as they are in the RAML spec. So the Resource has an Action member, which has a Response member. Inside the Response object is where I assumed I would find the example.

Here is what worked :

private void foo(Action _a,) {

        Collection<Response> responses = _a.getResponses().values();
        
        for (Response r : responses) {
            Collection<MimeType> mime = r.getBody().values();
            for(MimeType m : mime) {
                System.out.println(m.getExample());
            }
        }
    }

#5

That makes sense - good you’ve seen it first. Sorry for the delay in my response :blush:


#6

I tried your code and I still do not see the response code from the RAML file displayed. In my RAML file, under responses, I have 200: 400: 500: etc… but I can never find anywhere in the code to get the 200 from. It’s not part of the response, body, mimetypes, etc. It seems the data below each of the response codes is returned, and maybe the JavaScript parser has some way of getting at that since the documentation part of the designer shows it. But the Java RAML parser doesn’t seem to give you that code that I can see.

Basically I am trying to figure out how to build my methods (generated SDK code from RAML file) by looking for a 2xx response code type, then using the schema defined there to figure out the method response class to use.

I believe the JAX-RS module responds with a ResponseWrapper class and that’s the right way to do this since there is the possibility of error responses and other response bodies, but I am still trying to figure out how to pick out the specific 2xx response code in the RAML file. Any thoughts?


#7

Can you send me the RAML file you’ll try. I’ll have a look as soon as I have some time :wink:


#8

In the snippet below, I am trying to get the 200, 400, 401, 403 and 500 values. I have iterated the Response collection, and I can get everything else but the value of the response code. I think either I am missing some obscure way to access that, or it was not added to the RAML java parser code, in which case it probably should be added.

/users:
get:
responses:
200:
body:
application/json;version=1:
schema: users
example: !include example/json/users.json
400:
body:
application/json:
schema: errors
example: |
{
“status”: 400,
“code”: “E400-1”,
“message”: “Bad client info”,
}
401:
403:
500:
body:
application/json:
schema: errors


#9

Hi @justjacksonn - I tried it and I am getting the 200 back without any problem. So can you elaborate more on what you tried?