Skip to content

Conversation

@fmeurisse
Copy link

This commit add the possibility to define routing in a configuration file. Each line of the configuration file defines a line with the following format:
[HttpMethod] [paramType] [routePath] [controller static method]\n

@ybonnel
Copy link
Owner

ybonnel commented Sep 17, 2013

I don't like config by file (it's not compile time, and in case of refactoring it's hell).

But your pull request gave me an idea, use annotation :

public static class Controller {

    @Jsonp(callback = "CALLBACK", path = "/jsonp", resultType = String.class)
    @Get(path = "/resource", resultType = String.class)
    public static String helloWorld() {
        return "Hello World";
    }

    @Get(path = "/resource/:name", resultType = String.class)
    public static String helloName(@RouteParam("name") String name) throws HttpErrorException {
        if (name.equals("notfound")) {
            throw new HttpErrorException(404);
        }
        return "Hello " + name;
    }

    @Post(path = "/resource", paramType = String.class, resultType = String.class)
    @Put(path = "/resource/put", paramType = String.class, resultType = String.class)
    public static String helloPost(String name) {
        return "Hello " + name;
    }

    @Get(path = "/othercode", resultType = String.class)
    public static Response<String> iAmATeaPot() {
        return new Response<>("I m a teapot", 418);
    }

    @Delete(path = "/resource/delete", resultType = String.class)
    public static String delete() {
        return "deleted";
    }
}

And in the main :

setAnnotatedClasses(Controller.class);

Maybe also an annotation for resource, something like :

@Resource(path = "/member", type = Member.class)

What do you think?

@fmeurisse
Copy link
Author

I will try to implement that.

2013/9/17 Yan Bonnel notifications@github.com

I don't like config by file (it's not compile time, and in case of
refactoring it's hell).

But your pull request gave me an idea, use annotation :

public static class Controller {

@Jsonp(callback = "CALLBACK", path = "/jsonp", resultType = String.class)
@Get(path = "/resource", resultType = String.class)
public static String helloWorld() {
    return "Hello World";
}

@Get(path = "/resource/:name", resultType = String.class)
public static String helloName(@RouteParam("name") String name) throws HttpErrorException {
    if (name.equals("notfound")) {
        throw new HttpErrorException(404);
    }
    return "Hello " + name;
}

@Post(path = "/resource", paramType = String.class, resultType = String.class)
@Put(path = "/resource/put", paramType = String.class, resultType = String.class)
public static String helloPost(String name) {
    return "Hello " + name;
}

@Get(path = "/othercode", resultType = String.class)
public static Response<String> iAmATeaPot() {
    return new Response<>("I m a teapot", 418);
}

@Delete(path = "/resource/delete", resultType = String.class)
public static String delete() {
    return "deleted";
}

}

And in the main :

setAnnotatedClasses(Controller.class);

Maybe also an annotation for resource, something like :

@resource(path = "/member", type = Member.class)

What do you think?


Reply to this email directly or view it on GitHubhttps://github.com//pull/15#issuecomment-24585539
.

@ybonnel
Copy link
Owner

ybonnel commented Sep 17, 2013

I've created the branch "annotation" with my idea of annotations.

Test to fix : AnnotationIntegrationTest

I did not create the annotation for Resource, because i'm not sure how to do that (must extend RestResource, so there is a constructor to call, ...).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants