Reputation: 3091
I'm struggling to configure rest in spring. Springboot magic configuration works, but I'm not asking about that. I thought I have to add:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
anotate configuration with @EnableWebMvc and done. In most 'tutorials' it's like that. But that does not work. It fails on missing javax.servlet.ServletContext. I add it (no tutorial does that), and it fails on "no servlet context set".
What is wrong here? App even fails to boot.
maven:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>4.3.14.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
<version>2.10.0</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<!--<scope>provided</scope>-->
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<finalName>${finalName}-notShaded</finalName>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version>
<executions>
<!-- Run shade goal on package phase -->
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>${finalName}</finalName>
<transformers>
<!-- add Main-Class to manifest file -->
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
main:
try (AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext()) {
SimpleCommandLinePropertySource sclps = new SimpleCommandLinePropertySource(args);
ctx.getEnvironment().getPropertySources().addFirst(sclps);
ctx.register(Configuration.class);
ctx.refresh();
ctx.start();
} catch (Exception e) {
throw new SdpException("Error initializing spring", e);
}
configuration:
@org.springframework.context.annotation.Configuration
@ComponentScan(basePackages = {"..."})
@EnableWebMvc
public class Configuration {
}
Upvotes: 1
Views: 120
Reputation: 517
If you want to create a Spring webapp in a self-contained app manually, you can:
main
method. This will allow you to have your webapp executable as a jar).WebApplicationInitializer
which will be responsible to create and initialize the Spring contextIn your custom WebApplicationinitializer
class, you juste have to tell explicitely what are your configuration classes or scan a package you choose to find all @Configuration
classes.
I made a POC on Github based on this very good article. I hope this will help you.
Upvotes: 1