|
| 1 | +--- |
| 2 | +title: Spring Web Application Setup Without Boot |
| 3 | +lang: en |
| 4 | +layout: post |
| 5 | +audio: false |
| 6 | +translated: false |
| 7 | +generated: true |
| 8 | +image: false |
| 9 | +--- |
| 10 | + |
| 11 | +Got it. Since you’re using **Java 8** with **Spring 5.2.2** (no Spring Boot here, just core Spring + web), you’ll need to set up a minimal application manually. Below is a simple example showing how to build a web app with one controller. |
| 12 | + |
| 13 | +--- |
| 14 | + |
| 15 | +### 1. Add Required Dependencies |
| 16 | + |
| 17 | +You already have `spring-context`, `spring-web`, and `spring-core`. For serving requests, you also need `spring-webmvc`: |
| 18 | + |
| 19 | +```xml |
| 20 | +<dependency> |
| 21 | + <groupId>org.springframework</groupId> |
| 22 | + <artifactId>spring-webmvc</artifactId> |
| 23 | + <version>5.2.2.RELEASE</version> |
| 24 | +</dependency> |
| 25 | +``` |
| 26 | + |
| 27 | +--- |
| 28 | + |
| 29 | +### 2. Create a Controller |
| 30 | + |
| 31 | +```java |
| 32 | +package com.example.demo; |
| 33 | + |
| 34 | +import org.springframework.stereotype.Controller; |
| 35 | +import org.springframework.web.bind.annotation.*; |
| 36 | + |
| 37 | +@Controller |
| 38 | +public class HelloController { |
| 39 | + |
| 40 | + @ResponseBody |
| 41 | + @RequestMapping("/hello") |
| 42 | + public String hello() { |
| 43 | + return "Hello, Spring Web!"; |
| 44 | + } |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +* `@Controller` tells Spring this is a web controller. |
| 49 | +* `@RequestMapping("/hello")` maps to `http://localhost:8080/hello`. |
| 50 | +* `@ResponseBody` makes it return plain text (not a JSP). |
| 51 | + |
| 52 | +--- |
| 53 | + |
| 54 | +### 3. Define Spring Configuration |
| 55 | + |
| 56 | +Since we’re not using Spring Boot, we must wire things manually. |
| 57 | + |
| 58 | +```java |
| 59 | +package com.example.demo; |
| 60 | + |
| 61 | +import org.springframework.context.annotation.*; |
| 62 | +import org.springframework.web.servlet.config.annotation.*; |
| 63 | + |
| 64 | +@Configuration |
| 65 | +@EnableWebMvc |
| 66 | +@ComponentScan(basePackages = "com.example.demo") |
| 67 | +public class AppConfig implements WebMvcConfigurer { |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +This enables Spring MVC and scans your package for controllers. |
| 72 | + |
| 73 | +--- |
| 74 | + |
| 75 | +### 4. Set Up `web.xml` (for Tomcat/Servlet container) |
| 76 | + |
| 77 | +If you’re packaging as a WAR: |
| 78 | + |
| 79 | +```xml |
| 80 | +<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" |
| 81 | + version="3.1"> |
| 82 | + |
| 83 | + <servlet> |
| 84 | + <servlet-name>dispatcher</servlet-name> |
| 85 | + <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> |
| 86 | + <init-param> |
| 87 | + <param-name>contextClass</param-name> |
| 88 | + <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value> |
| 89 | + </init-param> |
| 90 | + <init-param> |
| 91 | + <param-name>contextConfigLocation</param-name> |
| 92 | + <param-value>com.example.demo.AppConfig</param-value> |
| 93 | + </init-param> |
| 94 | + <load-on-startup>1</load-on-startup> |
| 95 | + </servlet> |
| 96 | + |
| 97 | + <servlet-mapping> |
| 98 | + <servlet-name>dispatcher</servlet-name> |
| 99 | + <url-pattern>/</url-pattern> |
| 100 | + </servlet-mapping> |
| 101 | + |
| 102 | +</web-app> |
| 103 | +``` |
| 104 | + |
| 105 | +This sets up the `DispatcherServlet` and loads your `AppConfig`. |
| 106 | + |
| 107 | +--- |
| 108 | + |
| 109 | +### 5. Run the App |
| 110 | + |
| 111 | +* Package as a WAR and deploy to Tomcat (or Jetty). |
| 112 | +* Start Tomcat and visit: |
| 113 | + |
| 114 | + ``` |
| 115 | + http://localhost:8080/your-app/hello |
| 116 | + ``` |
| 117 | +* You should see: |
| 118 | + |
| 119 | + ``` |
| 120 | + Hello, Spring Web! |
| 121 | + ``` |
| 122 | + |
| 123 | +--- |
| 124 | + |
| 125 | +👉 Question for you: do you want this app to run inside **Tomcat (WAR deployment)**, or would you prefer a **standalone `main()` launcher** (embedded server, like Spring Boot but minimal)? I can show both ways depending on your setup. |
0 commit comments