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 jakarta.servlet.*;
020
021import org.apache.juneau.rest.annotation.*;
022import org.apache.juneau.rest.springboot.*;
023import org.springframework.boot.autoconfigure.SpringBootApplication;
024import org.springframework.boot.builder.*;
025import org.springframework.boot.web.servlet.*;
026import org.springframework.context.annotation.*;
027import org.springframework.stereotype.Controller;
028import org.springframework.web.filter.*;
029
030/**
031 * Entry point for Examples REST application when deployed as a Spring Boot application.
032 *
033 * <h5 class='section'>See Also:</h5><ul>
034 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/MySpringBootMicroserviceBasics">My SpringBoot Microservice Basics</a>
035 * </ul>
036 */
037@SpringBootApplication
038@Controller
039public class App {
040
041   /**
042    * Entry point method.
043    * @param args Command-line arguments.
044    */
045   @SuppressWarnings("resource")
046   public static void main(String[] args) {
047      new SpringApplicationBuilder(App.class).run(args);
048   }
049
050   /**
051    * Our root REST bean.
052    * <p>
053    * Note that this must extend from {@link SpringRestServlet} so that child resources can be resolved as Spring
054    * beans.
055    * <p>
056    * All REST objects are attached to this bean using the {@link Rest#children()} annotation.
057    *
058    * @return The root resources REST bean.
059    */
060   @Bean
061   public RootResources getRootResources() {
062      return new RootResources();
063   }
064
065   /**
066    * Optionally return the {@link HelloWorldResource} object as an injectable bean.
067    *
068    * @return The hello-world REST bean.
069    */
070   @Bean
071   public HelloWorldResource getHelloWorldResource() {
072      return new HelloWorldResource("Hello Spring user!");
073   }
074
075   /**
076    * Optionally return an injectable message provider for the {@link HelloWorldResource} class.
077    *
078    * @return The message provider for the hello-world REST bean.
079    */
080   @Bean
081   public HelloWorldMessageProvider getHelloWorldMessageProvider() {
082      return new HelloWorldMessageProvider("Hello Spring injection user!");
083   }
084
085   /**
086    * @param rootResources The root REST resource servlet
087    * @return The servlet registration mapped to "/*".
088    */
089   @Bean
090   public ServletRegistrationBean<Servlet> getRootServlet(RootResources rootResources) {
091      return new ServletRegistrationBean<>(rootResources, "/*");
092   }
093
094   /**
095    * We want to be able to consume url-encoded-form-post bodies, but HiddenHttpMethodFilter triggers the HTTP
096    * body to be consumed.  So disable it.
097    *
098    * @param filter The filter.
099    * @return Filter registration bean.
100    */
101   @Bean
102   public FilterRegistrationBean<HiddenHttpMethodFilter> registration(HiddenHttpMethodFilter filter) {
103      FilterRegistrationBean<HiddenHttpMethodFilter> registration = new FilterRegistrationBean<>(filter);
104      registration.setEnabled(false);
105      return registration;
106   }
107}