cagataygurturk / lambadaframework

Build serverless REST API's with JAVA. It implements the JAX-RS API and deploys your application easily to AWS Lambda and API Gateway
MIT License
244 stars 48 forks source link

Form and Post data support along with DefaultValue support #28

Open arranubels opened 8 years ago

arranubels commented 8 years ago

Hi,

I have made a couple improvements to the form data and post data changes, taking into consideration recent updates to master.

With this change the following methods work:

Objects

    public static class Input {
        public String value;

        public Input(String value) {
            this.value = value;
        }

        public Input() {
        }
    }

    @POST
    @Path("test/test1")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public Response test1(Input value) {

        logger.debug("Request got");
        return Response.status(201)
                .entity(value)
                .build();
    }

Request text

{ "value": "asdfasdf" }

Query:

++ curl -X POST -d @testfiles/object.txt https://xxxx.execute-api.eu-west-1.amazonaws.com/production/test/test1 --header 'Content-Type: APPLICATION/JSON'
+ echo '{"value":"asdfasdf"}'

Json text

    @POST
    @Path("test/test2")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public Response test2(String value) {

        logger.debug("Request got");
        return Response.status(201)
                .entity(value)
                .build();
    }

Request text:

"asdfasdf"

Query:

++ curl -X POST -d @testfiles/jsonstring.txt https://xxx.execute-api.eu-west-1.amazonaws.com/production/test/test2 --header 'Content-Type: APPLICATION/JSON'
+ echo '"asdfasdf"'
"asdfasdf"

Form param

    @POST
    @Path("test/test3")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public Response test3(@FormParam("value") String value) {

        logger.debug("Request got");
        return Response.status(201)
                .entity(value)
                .build();
    }

This can be used multiple ways:

Way 1

"value=asdfasdf"

Query

++ curl -X POST -d @testfiles/jsonformenc.txt https://xxxx.execute-api.eu-west-1.amazonaws.com/production/test/test3 --header 'Content-Type: APPLICATION/JSON'
+ echo '"asdfasdf"'
"asdfasdf"

Way 2

value=asdfasdf

Query

++ curl -X POST -d @testfiles/formenc.txt https://xxx.execute-api.eu-west-1.amazonaws.com/production/test/test3 --header 'Content-Type: APPLICATION/JSON'
+ echo '"asdfasdf"'
"asdfasdf"

Multiple values

    @POST
    @Path("test/test4")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public Response test4(@FormParam("value") List<String> value) {

        logger.debug("Request got");
        return Response.status(201)
                .entity(value)
                .build();
    }

Request text:

value=asdfasdf&value=12312312

Query

++ curl -X POST -d @testfiles/arrayformenc.txt https://xxxx.execute-api.eu-west-1.amazonaws.com/production/test/test4 --header 'Content-Type: APPLICATION/JSON'
+ echo '["asdfasdf","12312312"]'
["asdfasdf","12312312"]

Note

Please note: I don't do anything with @Produces, other @Consumes could be supported easily with some Api Gateway changes.

Please let me know if you need me to change anything.

cagataygurturk commented 8 years ago

I'll be looking into it as soon as possible, thanks

arran4 commented 8 years ago

Thanks

arranubels commented 8 years ago

Updated code in PR with latest from master. (And tested.)

cagataygurturk commented 8 years ago
arranubels commented 8 years ago

Tests now working, it was a type cast issue. I also included a log4j file which will be included only for test and modified the test class path to the default maven one so it didn't have to be explicitly mentioned. Happy to revert if there was a reason for it. (I couldn't see.)

I reverted it back to being a provided dependency, sorry I didn't know why it was provided.

arran4 commented 7 years ago

Ping.