Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example REST resource class for a Dropwizard application
/*
* Copyright (C) 2015 Marc Rufer (m.rufer@gmx.ch)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package be.rufer.playground.dropwizard.resource;
import com.codahale.metrics.annotation.Timed;
import com.google.common.base.Optional;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import java.util.concurrent.atomic.AtomicLong;
@Path("/example")
@Produces(MediaType.APPLICATION_JSON)
public class ExampleResource {
private final String template;
private final String defaultName;
private final AtomicLong counter;
public ExampleResource(String template, String defaultName) {
this.template = template;
this.defaultName = defaultName;
this.counter = new AtomicLong();
}
@GET
@Timed
public ResponseWrapper getExample(@QueryParam("name") Optional<String> name) {
final String value = String.format(template, name.or(defaultName));
return new ResponseWrapper(counter.incrementAndGet(), value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment