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 org.apache.juneau.rest.annotation.*;
020import org.apache.juneau.rest.springboot.*;
021import org.springframework.boot.autoconfigure.*;
022import org.springframework.boot.builder.*;
023import org.springframework.boot.web.servlet.*;
024import org.springframework.context.annotation.*;
025import org.springframework.stereotype.*;
026import org.springframework.web.filter.*;
027
028import jakarta.servlet.*;
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    * Optionally return an injectable message provider for the {@link HelloWorldResource} class.
052    *
053    * @return The message provider for the hello-world REST bean.
054    */
055   @Bean
056   public HelloWorldMessageProvider getHelloWorldMessageProvider() { return new HelloWorldMessageProvider("Hello Spring injection user!"); }
057
058   /**
059    * Optionally return the {@link HelloWorldResource} object as an injectable bean.
060    *
061    * @return The hello-world REST bean.
062    */
063   @Bean
064   public HelloWorldResource getHelloWorldResource() { return new HelloWorldResource("Hello Spring user!"); }
065
066   /**
067    * Our root REST bean.
068    * <p>
069    * Note that this must extend from {@link SpringRestServlet} so that child resources can be resolved as Spring
070    * beans.
071    * <p>
072    * All REST objects are attached to this bean using the {@link Rest#children()} annotation.
073    *
074    * @return The root resources REST bean.
075    */
076   @Bean
077   public RootResources getRootResources() { return new RootResources(); }
078
079   /**
080    * @param rootResources The root REST resource servlet
081    * @return The servlet registration mapped to "/*".
082    */
083   @Bean
084   public ServletRegistrationBean<Servlet> getRootServlet(RootResources rootResources) {
085      return new ServletRegistrationBean<>(rootResources, "/*");
086   }
087
088   /**
089    * We want to be able to consume url-encoded-form-post bodies, but HiddenHttpMethodFilter triggers the HTTP
090    * body to be consumed.  So disable it.
091    *
092    * @param filter The filter.
093    * @return Filter registration bean.
094    */
095   @Bean
096   public FilterRegistrationBean<HiddenHttpMethodFilter> registration(HiddenHttpMethodFilter filter) {
097      var registration = new FilterRegistrationBean<>(filter);
098      registration.setEnabled(false);
099      return registration;
100   }
101}