URI Rest with Httpservlet

Asked

Viewed 57 times

0

So, guys, I’m developing a mini framework for study purposes, using the Java front controller, and I came across the following problem.

First of all I’ll explain how it’s working.

I embed the Jetty server inside the framework, when I open my application (using the mini-framework) it will scan the classes of my project check if there are two annotations that I created in my classes that are @Rest (defines the class as a controller) and @Get (maps the request) 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 play it in the key of my map and I take the value that is the controller class and I invoke the beauty method?

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

Format I want to implement:

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

and not

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

My question is: How to get there in my Serrvlet that is implemented the value of :id ?

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

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

Follow the project link on github: https://github.com/NetoDevel/riley-framework

  • The answer helped you or needs more details?

1 answer

0

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

Map pathVariables = (Map) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
  • It didn’t work and this Handlermapping class is from spring? the idea is to implement itself.

  • @Josevieiraneto I got it, so check this link http://stackoverflow.com/a/8715566/5230740.

  • I had already seen this question, but it is something difficult to manipulate because it makes split per bar, it will be difficult to know who are the parameters in Ri. ex: /Books/1/Author/1 when I do the split it will break into 4 indices Books, 1, Author, 1 blz? How to know who is the param of Uri?

  • In each URI you have an expected format. In this case, param is book=Indice[1] and Author=Indice[3]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.