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.petstore.springboot;
018
019import org.apache.juneau.rest.server.*;
020import org.apache.juneau.rest.server.servlet.*;
021import org.springframework.beans.factory.annotation.*;
022
023/**
024 * Tiny REST resource that demonstrates Spring {@code @Autowired} injection into a Juneau {@code @Rest} servlet
025 * under a Spring Boot deployment.  Mirrors the legacy {@code HelloWorldResource} demo.
026 *
027 * <p>
028 * The {@link HelloMessageProvider} bean is wired by Spring; the {@code GET /hello/*} endpoint surfaces its message.
029 * Resolution of the {@code HelloMessageProvider} bean works because the parent router extends
030 * {@code BasicSpringRestServletGroup} (i.e. {@code SpringRestServlet}), which lets child resources be Spring beans.
031 *
032 * <h5 class='section'>See Also:</h5><ul>
033 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauPetstore">juneau-petstore</a>
034 * </ul>
035 */
036@Rest(
037   path="/hello",
038   title="Hello (Spring injection demo)",
039   description="A trivial REST resource demonstrating Spring @Autowired injection of a HelloMessageProvider bean."
040)
041public class HelloResource extends BasicRestResource {
042
043   private final HelloMessageProvider messageProvider;
044
045   /**
046    * Constructor.
047    *
048    * @param messageProvider The Spring-injected message provider bean.
049    */
050   @Autowired
051   public HelloResource(HelloMessageProvider messageProvider) {
052      this.messageProvider = messageProvider;
053   }
054
055   /**
056    * GET /hello/* — returns the message provided by the injected Spring bean.
057    *
058    * @return The injected message.
059    */
060   @RestGet(path="/*", summary="Responds with the Spring-injected message.")
061   public String sayHello() {
062      return messageProvider.get();
063   }
064}