{"id":20162110,"url":"https://github.com/megadev007/2015-public-spring-guides","last_synced_at":"2025-03-17T19:32:40.252Z","repository":{"id":234119963,"uuid":"788302717","full_name":"MegaDev007/2015-public-spring-guides","owner":"MegaDev007","description":null,"archived":false,"fork":false,"pushed_at":"2024-04-18T06:52:13.000Z","size":750,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-24T06:44:25.700Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MegaDev007.png","metadata":{"files":{"readme":"README.adoc","changelog":null,"contributing":"CONTRIBUTING.adoc","funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2024-04-18T06:47:50.000Z","updated_at":"2025-01-10T21:53:11.000Z","dependencies_parsed_at":"2024-04-18T08:06:47.481Z","dependency_job_id":null,"html_url":"https://github.com/MegaDev007/2015-public-spring-guides","commit_stats":null,"previous_names":["devcrazer/devcrazer2015-public-spring-guides-gs-spring-boot-public","kristoffalaric/2015-public-spring-guides","megadev007/2015-public-spring-guides"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MegaDev007%2F2015-public-spring-guides","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MegaDev007%2F2015-public-spring-guides/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MegaDev007%2F2015-public-spring-guides/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MegaDev007%2F2015-public-spring-guides/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MegaDev007","download_url":"https://codeload.github.com/MegaDev007/2015-public-spring-guides/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244096974,"owners_count":20397513,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-14T00:23:19.625Z","updated_at":"2025-03-17T19:32:40.220Z","avatar_url":"https://github.com/MegaDev007.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":":spring_boot_version: 2.5.0\n:spring-boot: https://github.com/spring-projects/spring-boot\n:toc:\n:icons: font\n:source-highlighter: prettify\n:project_id: gs-spring-boot\n\nThis guide provides a sampling of how {spring-boot}[Spring Boot] helps you accelerate\napplication development. As you read more Spring Getting Started guides, you will see more\nuse cases for Spring Boot. This guide is meant to give you a quick taste of Spring Boot.\nIf you want to create your own Spring Boot-based project, visit\nhttps://start.spring.io/[Spring Initializr], fill in your project details, pick your\noptions, and download a bundled up project as a zip file.\n\n== What You Will build\n\nYou will build a simple web application with Spring Boot and add some useful services to\nit.\n\n== What You Need\n\n:java_version: 1.8\ninclude::https://raw.githubusercontent.com/spring-guides/getting-started-macros/main/prereq_editor_jdk_buildtools.adoc[]\n\ninclude::https://raw.githubusercontent.com/spring-guides/getting-started-macros/main/how_to_complete_this_guide.adoc[]\n\n== Learn What You Can Do with Spring Boot\n\nSpring Boot offers a fast way to build applications. It looks at your classpath and at the\nbeans you have configured, makes reasonable assumptions about what you are missing, and\nadds those items. With Spring Boot, you can focus more on business features and less on\ninfrastructure.\n\nThe following examples show what Spring Boot can do for you:\n\n- Is Spring MVC on the classpath? There are several specific beans you almost always need,\nand Spring Boot adds them automatically. A Spring MVC application also needs a servlet\ncontainer, so Spring Boot automatically configures embedded Tomcat.\n- Is Jetty on the classpath? If so, you probably do NOT want Tomcat but instead want\nembedded Jetty. Spring Boot handles that for you.\n- Is Thymeleaf on the classpath? If so, there are a few beans that must always be added to\nyour application context. Spring Boot adds them for you.\n\nThese are just a few examples of the automatic configuration Spring Boot provides. At the\nsame time, Spring Boot does not get in your way. For example, if Thymeleaf is on your\npath, Spring Boot automatically adds a `SpringTemplateEngine` to your application context.\nBut if you define your own `SpringTemplateEngine` with your own settings, Spring Boot does\nnot add one. This leaves you in control with little effort on your part.\n\nNOTE: Spring Boot does not generate code or make edits to your files. Instead, when you\nstart your application, Spring Boot dynamically wires up beans and settings and applies\nthem to your application context.\n\n[[scratch]]\n== Starting with Spring Initializr\n\nYou can use this https://start.spring.io/#!type=maven-project\u0026language=java\u0026platformVersion=2.5.5\u0026packaging=jar\u0026jvmVersion=11\u0026groupId=com.example\u0026artifactId=spring-boot\u0026name=spring-boot\u0026description=Demo%20project%20for%20Spring%20Boot\u0026packageName=com.example.spring-boot\u0026dependencies=web[pre-initialized project] and click Generate to download a ZIP file. This project is configured to fit the examples in this tutorial.\n\nTo manually initialize the project:\n\n. Navigate to https://start.spring.io.\nThis service pulls in all the dependencies you need for an application and does most of the setup for you.\n. Choose either Gradle or Maven and the language you want to use. This guide assumes that you chose Java.\n. Click *Dependencies* and select *Spring Web*.\n. Click *Generate*.\n. Download the resulting ZIP file, which is an archive of a web application that is configured with your choices.\n\nNOTE: If your IDE has the Spring Initializr integration, you can complete this process from your IDE.\n\nNOTE: You can also fork the project from Github and open it in your IDE or other editor.\n\n[[initial]]\n== Create a Simple Web Application\n\nNow you can create a web controller for a simple web application, as the following listing\n(from `src/main/java/com/example/springboot/HelloController.java`) shows:\n\n====\n[source,java]\n----\ninclude::initial/src/main/java/com/example/springboot/HelloController.java[]\n----\n====\n\nThe class is flagged as a `@RestController`, meaning it is ready for use by Spring MVC to\nhandle web requests. `@GetMapping` maps `/` to the `index()` method. When invoked from\na browser or by using curl on the command line, the method returns pure text. That is\nbecause `@RestController` combines `@Controller` and `@ResponseBody`, two annotations that\nresults in web requests returning data rather than a view.\n\n== Create an Application class\n\nThe Spring Initializr creates a simple application class for you. However, in this case,\nit is too simple. You need to modify the application class to match the following listing\n(from `src/main/java/com/example/springboot/Application.java`):\n\n====\n[source,java]\n----\ninclude::complete/src/main/java/com/example/springboot/Application.java[]\n----\n====\n\ninclude::https://raw.githubusercontent.com/spring-guides/getting-started-macros/main/spring-boot-application-new-path.adoc[]\n\nThere is also a `CommandLineRunner` method marked as a `@Bean`, and this runs on start up.\nIt retrieves all the beans that were created by your application or that were\nautomatically added by Spring Boot. It sorts them and prints them out.\n\n== Run the Application\n\nTo run the application, run the following command in a terminal window (in the `complete`)\ndirectory:\n\n====\n[subs=\"attributes\"]\n----\n./gradlew bootRun\n----\n====\n\nIf you use Maven, run the following command in a terminal window (in the `complete`)\ndirectory:\n\n====\n[subs=\"attributes\"]\n----\n./mvnw spring-boot:run\n----\n====\n\nYou should see output similar to the following:\n\n====\n[source,text]\n----\nLet's inspect the beans provided by Spring Boot:\napplication\nbeanNameHandlerMapping\ndefaultServletHandlerMapping\ndispatcherServlet\nembeddedServletContainerCustomizerBeanPostProcessor\nhandlerExceptionResolver\nhelloController\nhttpRequestHandlerAdapter\nmessageSource\nmvcContentNegotiationManager\nmvcConversionService\nmvcValidator\norg.springframework.boot.autoconfigure.MessageSourceAutoConfiguration\norg.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration\norg.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration\norg.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration$DispatcherServletConfiguration\norg.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration$EmbeddedTomcat\norg.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration\norg.springframework.boot.context.embedded.properties.ServerProperties\norg.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor\norg.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor\norg.springframework.context.annotation.internalAutowiredAnnotationProcessor\norg.springframework.context.annotation.internalCommonAnnotationProcessor\norg.springframework.context.annotation.internalConfigurationAnnotationProcessor\norg.springframework.context.annotation.internalRequiredAnnotationProcessor\norg.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration\npropertySourcesBinder\npropertySourcesPlaceholderConfigurer\nrequestMappingHandlerAdapter\nrequestMappingHandlerMapping\nresourceHandlerMapping\nsimpleControllerHandlerAdapter\ntomcatEmbeddedServletContainerFactory\nviewControllerHandlerMapping\n----\n====\n\nYou can clearly see `org.springframework.boot.autoconfigure` beans. There is also a `tomcatEmbeddedServletContainerFactory`.\n\nNow run the service with curl (in a separate terminal window), by running the following\ncommand (shown with its output):\n\n====\n[source,text]\n----\n$ curl localhost:8080\nGreetings from Spring Boot!\n----\n====\n\n== Add Unit Tests\n\nYou will want to add a test for the endpoint you added, and Spring Test provides some\nmachinery for that.\n\nIf you use Gradle, add the following dependency to your `build.gradle` file:\n\n[source,groovy,indent=0]\n----\ninclude::complete/build.gradle[tag=tests]\n----\n\nIf you use Maven, add the following to your `pom.xml` file:\n\n[source,xml,indent=0]\n----\ninclude::complete/pom.xml[tag=tests]\n----\n\nNow write a simple unit test that mocks the servlet request and response through your\nendpoint, as the following listing (from\n`src/test/java/com/example/springboot/HelloControllerTest.java`) shows:\n\n====\n[source,java]\n----\ninclude::complete/src/test/java/com/example/springboot/HelloControllerTest.java[]\n----\n====\n\n`MockMvc` comes from Spring Test and lets you, through a set of convenient builder\nclasses, send HTTP requests into the `DispatcherServlet` and make assertions about the\nresult. Note the use of `@AutoConfigureMockMvc` and `@SpringBootTest` to inject a\n`MockMvc` instance. Having used `@SpringBootTest`, we are asking for the whole application\ncontext to be created. An alternative would be to ask Spring Boot to create only the web\nlayers of the context by using `@WebMvcTest`. In either case, Spring Boot automatically\ntries to locate the main application class of your application, but you can override it or\nnarrow it down if you want to build something different.\n\nAs well as mocking the HTTP request cycle, you can also use Spring Boot to write a simple\nfull-stack integration test. For example, instead of (or as well as) the mock test shown\nearlier, we could create the following test (from\n`src/test/java/com/example/springboot/HelloControllerIT.java`):\n\n====\n[source,java]\n----\ninclude::complete/src/test/java/com/example/springboot/HelloControllerIT.java[]\n----\n====\n\nThe embedded server starts on a random port because of\n`webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT`, and the actual port is\nconfigured automatically in the base URL for the `TestRestTemplate`.\n\n== Add Production-grade Services\n\nIf you are building a web site for your business, you probably need to add some management\nservices. Spring Boot provides several such services (such as health, audits, beans, and\nmore) with its\nhttp://docs.spring.io/spring-boot/docs/{spring_boot_version}/reference/htmlsingle/#production-ready[actuator module].\n\nIf you use Gradle, add the following dependency to your `build.gradle` file:\n\n[source,groovy,indent=0]\n----\ninclude::complete/build.gradle[tag=actuator]\n----\n\nIf you use Maven, add the following dependency to your `pom.xml` file:\n\n[source,xml,indent=0]\n----\ninclude::complete/pom.xml[tag=actuator]\n----\n\nThen restart the application. If you use Gradle, run the following command in a terminal\nwindow (in the `complete` directory):\n\n====\n[subs=\"attributes\"]\n----\n./gradlew bootRun\n----\n====\n\nIf you use Maven, run the following command in a terminal window (in the `complete`\ndirectory):\n\n====\n[subs=\"attributes\"]\n----\n./mvnw spring-boot:run\n----\n====\n\nYou should see that a new set of RESTful end points have been added to the application.\nThese are management services provided by Spring Boot. The following listing shows typical\noutput:\n\n====\n[source,text]\n----\nmanagement.endpoint.configprops-org.springframework.boot.actuate.autoconfigure.context.properties.ConfigurationPropertiesReportEndpointProperties\nmanagement.endpoint.env-org.springframework.boot.actuate.autoconfigure.env.EnvironmentEndpointProperties\nmanagement.endpoint.health-org.springframework.boot.actuate.autoconfigure.health.HealthEndpointProperties\nmanagement.endpoint.logfile-org.springframework.boot.actuate.autoconfigure.logging.LogFileWebEndpointProperties\nmanagement.endpoints.jmx-org.springframework.boot.actuate.autoconfigure.endpoint.jmx.JmxEndpointProperties\nmanagement.endpoints.web-org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties\nmanagement.endpoints.web.cors-org.springframework.boot.actuate.autoconfigure.endpoint.web.CorsEndpointProperties\nmanagement.health.diskspace-org.springframework.boot.actuate.autoconfigure.system.DiskSpaceHealthIndicatorProperties\nmanagement.info-org.springframework.boot.actuate.autoconfigure.info.InfoContributorProperties\nmanagement.metrics-org.springframework.boot.actuate.autoconfigure.metrics.MetricsProperties\nmanagement.metrics.export.simple-org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleProperties\nmanagement.server-org.springframework.boot.actuate.autoconfigure.web.server.ManagementServerProperties\n----\n====\n\nThe actuator exposes the following:\n\n* http://localhost:8080/actuator/health[actuator/health]\n* http://localhost:8080/actuator[actuator]\n\nNOTE: There is also an `/actuator/shutdown` endpoint, but, by default, it is visible only\nthrough JMX. To http://docs.spring.io/spring-boot/docs/{spring_boot_version}/reference/htmlsingle/#production-ready-endpoints-enabling-endpoints[enable it as an HTTP endpoint], add\n`management.endpoint.shutdown.enabled=true` to your `application.properties` file\nand expose it with `management.endpoints.web.exposure.include=health,info,shutdown`.\nHowever, you probably should not enable the shutdown endpoint for a publicly available\napplication.\n\nYou can check the health of the application by running the following command:\n\n====\n[source,bash]\n----\n$ curl localhost:8080/actuator/health\n{\"status\":\"UP\"}\n----\n====\n\nYou can try also to invoke shutdown through curl, to see what happens when you have not\nadded the necessary line (shown in the preceding note) to `application.properties`:\n\n====\n[source,bash]\n----\n$ curl -X POST localhost:8080/actuator/shutdown\n{\"timestamp\":1401820343710,\"error\":\"Not Found\",\"status\":404,\"message\":\"\",\"path\":\"/actuator/shutdown\"}\n----\n====\n\nBecause we did not enable it, the requested endpoint is not available (because the endpoint does not\nexist).\n\nFor more details about each of these REST endpoints and how you can tune their settings\nwith an `application.properties` file (in `src/main/resources`), see the\nthe http://docs.spring.io/spring-boot/docs/{spring_boot_version}/reference/htmlsingle/#production-ready-endpoints[documentation about the endpoints].\n\n== View Spring Boot's Starters\n\nYou have seen some of\nhttp://docs.spring.io/spring-boot/docs/{spring_boot_version}/reference/htmlsingle/#using-boot-starter[Spring Boot's \"`starters`\"].\nYou can see them all\nhttps://github.com/spring-projects/spring-boot/tree/main/spring-boot-project/spring-boot-starters[here in source code].\n\n== JAR Support and Groovy Support\n\nThe last example showed how Spring Boot lets you wire beans that you may not be aware you\nneed. It also showed how to turn on convenient management services.\n\nHowever, Spring Boot does more than that. It supports not only traditional WAR file\ndeployments but also lets you put together executable JARs, thanks to Spring Boot's loader\nmodule. The various guides demonstrate this dual support through the\n`spring-boot-gradle-plugin` and `spring-boot-maven-plugin`.\n\nOn top of that, Spring Boot also has Groovy support, letting you build Spring MVC web\napplications with as little as a single file.\n\nCreate a new file called `app.groovy` and put the following code in it:\n\n====\n[source,java]\n----\n@RestController\nclass ThisWillActuallyRun {\n\n    @GetMapping(\"/\")\n    String home() {\n        return \"Hello, World!\"\n    }\n\n}\n----\n====\n\nNOTE: It does not matter where the file is. You can even fit an application that small\ninside a https://twitter.com/rob_winch/status/364871658483351552[single tweet]!\n\nNext, https://docs.spring.io/spring-boot/docs/{spring_boot_version}/reference/htmlsingle/#getting-started-installing-the-cli[install Spring Boot's CLI].\n\nRun the Groovy application by running the following command:\n\n====\n[source,bash]\n----\n$ spring run app.groovy\n----\n====\n\nNOTE: Shut down the previous application, to avoid a port collision.\n\nFrom a different terminal window, run the following curl command (shown with its output):\n\n====\n[source,bash]\n----\n$ curl localhost:8080\nHello, World!\n----\n====\n\nSpring Boot does this by dynamically adding key annotations to your code and using\nhttp://www.groovy-lang.org/Grape[Groovy Grape] to pull down the libraries that are needed\nto make the app run.\n\n== Summary\n\nCongratulations! You built a simple web application with Spring Boot and learned how it\ncan ramp up your development pace. You also turned on some handy production services.\nThis is only a small sampling of what Spring Boot can do. See\nhttp://docs.spring.io/spring-boot/docs/current/reference/htmlsingle[Spring Boot's online docs]\nfor much more information.\n\n== See Also\n\nThe following guides may also be helpful:\n\n* https://spring.io/guides/gs/securing-web/[Securing a Web Application]\n* https://spring.io/guides/gs/serving-web-content/[Serving Web Content with Spring MVC]\n\ninclude::https://raw.githubusercontent.com/spring-guides/getting-started-macros/main/footer.adoc[]\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmegadev007%2F2015-public-spring-guides","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmegadev007%2F2015-public-spring-guides","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmegadev007%2F2015-public-spring-guides/lists"}