Skip to content

parameter

Roland Schuller edited this page Apr 4, 2019 · 1 revision

Parameters

Of course every RestAPI needs to take parameters. Complicated? No!

Lets start with an simple example which has everything in it. A certain Blogpost has multiple images. Lets make an Endpoint that gets a certain image from a certain Blogpost.

The url should be like http://localhost:3000/blog/15/2 . Blog number 15, Image number 2.

RestHttpServer server=RestHttpServer.Start(3000);
server.getRootEndpoint().addSubPath("blog").addSubPath(":blogId").addRestEndpoint(new BlogImage(":imageId"));

//...
public class BlogImage extends GetEndpoint{

    public BlogImage(String endpointName) {
        super(endpointName);
    }

    @Override
    public void Call(Conversion conversion, Map<String, String> UrlParameter) {
        String blogId=UrlParameter.get("blogId");
        String imageId=UrlParameter.get("imageId");
        // .. your code ..
        conversion.getResponse().setData(imageByteData);
        conversion.getResponse().setContentType(ContentType.JPEG);
        //or
        conversion.getResponse().guessandSetContentTypefromData(ContentType.JPEG);
    }
    
}

The ":blogId" and ":imageId" are parameters or Placeholders for the request and be read in the Endpoint by name;

Clone this wiki locally