URI Rest with HttpServlet

0

So, I'm developing a mini framework for study purposes, using the pattern front controller in java, and I came across the following problem.

First of all, I'll explain how it works.

I embedded the jetty server inside the framework, when I starto my application (using the mini-framework) it will sweep the classes of my project check if there are two annotations that I created in my classes that are @Rest (define that the class is a controller) and @Get (it does the request mapping) and plays in a hashMap with the mapping as key and the controller class as value.

@Rest
public class UserController {

    @Get("/books")
    public String getBook() {
        Book book = new Book();
        book.setAuthor("NetoDevel");
        book.setTitle("RileyFramework");
        book.setPrice(0.0d);
        return JsonReturn.toJson(book);
    }

    @Get("/book/:id")
    public String findOne(String id) {
        return id;
    }
}

When I receive the request, I set it on the key of my map and get the value that is the controller class and instantiate and invoke the beauty method?

So far everything works fine, but I want to set parameters in my URI, in the REST architecture.

Format I want to implement:

http://localhost:8080/books/:id

and not

http://localhost:8080/books?id=1

My question is: How do I get the value of :id in my servlet?

I can have more than one param in this format below:

http://localhost:8080/books/:id/author/:id

Follow the project link in github: link

    
asked by anonymous 02.03.2017 / 02:51

1 answer

0

Get the map with all the parameters sent in request through the following code:

Map pathVariables = (Map) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
    
02.03.2017 / 03:32