/** * @author Graeme Rocher * @since 1.0 */ @Controller("/secure") public class SecureController { @Get public HttpResponse index(String username) { return HttpResponse.ok("Authenticated: " + username); } }
@Controller("/pets") public class PetController { @Get("/{name}") public HttpResponse<Pet> get(String name, @Header("X-Pet-Client") String clientId) { Pet pet = new Pet(); pet.setName(name); pet.setAge(Integer.valueOf(clientId)); return HttpResponse.ok(pet) .header("X-Pet-Client", clientId); } }
@Controller("/story") public class StoryController { @Get("/{id}") public HttpResponse<Story> get(String id) { Story story = new Story(); story.setId(id); return HttpResponse.ok(story); } }
@Controller("/convert") public class FooBarController { @Get("/foo/{foo}") public Foo foo(@PathVariable(name = "foo") Foo foo) { return foo; } @Get("/bar/{bar}") public Bar bar(@PathVariable(name = "bar") Bar bar) { return bar; } }
/** * @author graemerocher * @since 1.0 */ @Controller("/amazon") public class BookController { @Post(value = "/book/{title}", consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_FORM_URLENCODED}) @Status(HttpStatus.CREATED) Book save(String title) { return new Book(title); } }
/** * @author graemerocher * @since 1.0 */ @Controller("/binding") public class BookController { @Post(value = "/book", consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_FORM_URLENCODED}) @Status(HttpStatus.CREATED) Book save(@Body Book book) { return book; } }
/** * @author graemerocher * @since 1.0 */ // tag::class[] @Controller("/pets") class PetController implements PetOperations { @Override public Single<Pet> save(String name, int age) { Pet pet = new Pet(); pet.setName(name); pet.setAge(age); // save to database or something return Single.just(pet); } } // end::class[]
@Controller("/streaming/sse") public class HeadlineController { // tag::streaming[] @Get(value = "/headlines", produces = MediaType.TEXT_EVENT_STREAM) // <1> Flux<Event<Headline>> streamHeadlines() { return Flux.<Event<Headline>>create((emitter) -> { // <2> Headline headline = new Headline(); headline.setText("Latest Headline at " + ZonedDateTime.now()); emitter.next(Event.of(headline)); emitter.complete(); }).repeat(100) // <3> .delayElements(Duration.ofSeconds(1)); // <4> } // end::streaming[] }
/** * @author graemerocher * @since 1.0 */ @Controller("/streaming") public class HeadlineController { // tag::streaming[] @Get(value = "/headlines", produces = MediaType.APPLICATION_JSON_STREAM) // <1> Flowable<Headline> streamHeadlines() { return Flowable.fromCallable(() -> { // <2> Headline headline = new Headline(); headline.setText("Latest Headline at " + ZonedDateTime.now()); return headline; }).repeat(100) // <3> .delay(1, TimeUnit.SECONDS); // <4> } // end::streaming[] }
@Controller("/template") public class TemplateController {
@Requires(property = "spec.name", value = "httpstatus") @Controller("/httpResponseStatus") public class HttpResponseStatusController { //tag::httpResponseStatus[] @Get(produces = MediaType.TEXT_PLAIN) public HttpResponse index() { return HttpResponse.status(HttpStatus.CREATED).body("success"); } //end::httpResponseStatus[] }
@Controller("/spring-cloud/issues") public class SpringCloudConfigDemoController { @Value("${environment-name:LOCAL}") protected String appPrefix; @Get("/{number}") String issue(Integer number) { return appPrefix + ": issue # " + number + "!"; } }
@Requires(property = "spec.name", value = "AtErrorExceptionHandlerPrecedenceSpec") //tag::clazz[] @Controller("/books") public class BookController { @Produces(MediaType.TEXT_PLAIN) @Get("/stock/{isbn}") Integer stock(String isbn) { throw new OutOfStockException(); } } //end::clazz[]
@Requires(property = "spec.name", value = "AtErrorExceptionHandlerPrecedenceSpec") @Controller("/outofstock") public class OutOfStockController { @Error(exception = OutOfStockException.class, global = true) public HttpResponse handleOutOfStock(HttpRequest request) { return HttpResponse.ok(-1); } }
@Requires(property = "spec.name", value = "ExceptionHandlerSpec") //tag::clazz[] @Controller("/books") public class BookController { @Produces(MediaType.TEXT_PLAIN) @Get("/stock/{isbn}") Integer stock(String isbn) { throw new OutOfStockException(); } } //end::clazz[]
@Requires(property = "spec.name", value = "httpstatus") @Controller("/respondHttpStatus") public class RespondHttpStatusController { //tag::respondHttpStatus[] @Get public HttpStatus index() { return HttpStatus.CREATED; } //end::respondHttpStatus[] }
@Requires(property = "spec.name", value = "respondingnotfound") //tag::clazz[] @Controller("/books") public class BooksController { @Get("/stock/{isbn}") public Map stock(String isbn) { return null; //<1> } @Get("/maybestock/{isbn}") public Maybe<Map> maybestock(String isbn) { return Maybe.empty(); //<2> } } //end::clazz[]
@Requires(property = "spec.name", value = "BindHttpClientExceptionBodySpec") //tag::clazz[] @Controller("/books") public class BooksController { @Get("/{isbn}") public HttpResponse find(String isbn) { if (isbn.equals("1680502395")) { Map<String, Object> m = new HashMap<>(); m.put("status", 401); m.put("error", "Unauthorized"); m.put("message", "No message available"); m.put("path", "/books/"+isbn); return HttpResponse.status(HttpStatus.UNAUTHORIZED).body(m); } return HttpResponse.ok(new Book("1491950358", "Building Microservices")); } } //end::clazz[]
@Requires(property = "spec.name", value = "httpstatus") @Controller("/status") public class StatusController { @Get(value = "/simple", produces = MediaType.TEXT_PLAIN) public String simple() { return "success"; } //tag::atstatus[] @Status(HttpStatus.CREATED) @Get(produces = MediaType.TEXT_PLAIN) public String index() { return "success"; } //end::atstatus[] @Status(HttpStatus.CREATED) @Get(value = "/voidreturn") public void voidReturn() { } @Status(HttpStatus.NOT_FOUND) @Get(value = "/simple404", produces = MediaType.TEXT_PLAIN) public String simple404() { return "success"; } }
@Controller("/") @Requires(property = MockSpringCloudConfigServer.ENABLED) public class MockSpringCloudConfigServer implements SpringCloudConfigOperations {