001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.microservice.springboot.template;
018
019import java.util.*;
020
021import org.apache.juneau.html.annotation.*;
022import org.apache.juneau.rest.annotation.*;
023import org.apache.juneau.rest.servlet.BasicRestObject;
024import org.springframework.beans.factory.annotation.Autowired;
025
026/**
027 * Sample REST resource that prints out a simple "Hello world!" message.
028 *
029 * <h5 class='section'>See Also:</h5><ul>
030 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/MySpringBootMicroserviceBasics">My SpringBoot Microservice Basics</a>
031
032 * </ul>
033 */
034@Rest(
035   title="Hello World",
036   description="An example of the simplest-possible resource",
037   path="/helloWorld"
038)
039@HtmlDocConfig(
040   aside={
041      "<div style='max-width:400px' class='text'>",
042      "  <p>This page shows a resource that simply response with a 'Hello world!' message</p>",
043      "  <p>The POJO serialized is a simple String.</p>",
044      "</div>"
045   }
046)
047public class HelloWorldResource extends BasicRestObject {
048
049   private final String message;
050
051   /**
052    * Optional message provider that can be injected into this object.
053    */
054   @Autowired
055   private Optional<HelloWorldMessageProvider> messageProvider;
056
057   /**
058    * Default constructor.
059    * <p>
060    * Used by default if bean cannot be found in the Spring application context.
061    */
062   public HelloWorldResource() {
063      this("Hello world!");
064   }
065
066   /**
067    * Constructor.
068    *
069    * @param message The message to display.
070    */
071   public HelloWorldResource(String message) {
072      this.message = message;
073   }
074
075   /**
076    * GET request handler.
077    *
078    * @return A simple Hello-World message.
079    */
080   @RestGet(path="/*", summary="Responds with \"Hello world!\"")
081   public String sayHello() {
082      String message = this.message;
083      if (messageProvider != null && messageProvider.isPresent())
084         message = messageProvider.get().get();
085      return message;
086   }
087}