Spring 4 Annotation Configuration without web.xml file using WebApplicationInitializer - Java @ Desk

Saturday, February 11, 2017

Spring 4 Annotation Configuration without web.xml file using WebApplicationInitializer

Spring 4 Annotation Configuration without web.xml file using WebApplicationInitializer

Spring 4 allows annotation based configuration to initialize the web application without having the web.xml as well as spring configuration xml file. The spring controller and services are loaded using the annotation @ComponentScan.

With servlet 3.x there is no need of the spring configuration file defined in web.xml. Servlet 3.x allows annotation based loading of configuration classes for controllers and services.

Below example will load the controllers and services classes on server start up i.e. there will be a class to replace web.xml to map the Spring Dispatcher Servlet and load it on server start up. Rest controller example is implemented here that returns the JSON response to the caller.

pom.xml - Add Spring 4.x release and Servlet 3.x release to support annotation based loading of Dispatcher Servlet
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>Blogger</groupId>
 <artifactId>Blogger</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>war</packaging>
 <build>
  <finalName>blogger</finalName>
  <sourceDirectory>src</sourceDirectory>
  <plugins>
   <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.5.1</version>
    <configuration>
     <source>1.8</source>
     <target>1.8</target>
    </configuration>
   </plugin>
   <plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>3.0.0</version>
    <configuration>
     <warSourceDirectory>WebContent</warSourceDirectory>
    </configuration>
   </plugin>
  </plugins>
 </build>
 <dependencies>
  <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>javax.servlet-api</artifactId>
   <version>3.1.0</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-webmvc</artifactId>
   <version>4.3.1.RELEASE</version>
  </dependency>
  <dependency>
   <groupId>com.fasterxml.jackson.core</groupId>
   <artifactId>jackson-databind</artifactId>
   <version>2.7.5</version>
  </dependency>
 </dependencies>
</project>


AppConfig.java - This class bootstrap Spring 4 MVC Rest in our application on server startup
package com.rest.spring4.annotationconfiguration.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.rest.spring4.annotationconfiguration")
public class AppConfig {

}


AppInitializer.java - This class is loaded by Servlet 3.x. This class will replace web.xml and it will map the spring’s dispatcher servlet and bootstrap it.
package com.rest.spring4.annotationconfiguration.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
 @Override
 protected Class[] getRootConfigClasses() {
  return new Class[] { AppConfig.class };
 }

 @Override
 protected Class[] getServletConfigClasses() {
  return null;
 }

 @Override
 protected String[] getServletMappings() {
  return new String[] { "/" };
 }
}




Person.java - Domain object for JSON response
package com.rest.spring4.annotationconfiguration.pojo;

public class Person {

 private String id;

 private String name;

 public String getId() {
  return id;
 }

 public void setId(String id) {
  this.id = id;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }
}


PersonRestController.java - This class contains the Rest mapping to load the list of person objects in JSON format
package com.rest.spring4.annotationconfiguration.restcontroller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import com.rest.spring4.annotationconfiguration.pojo.Person;

@RestController
public class PersonRestController {

 @GetMapping("/persons")
 public ResponseEntity getPersons() {
  List<Person> persons = new ArrayList<Person>();

  Person person = new Person();
  person.setId("Id 1");
  person.setName("Person Name 1");

  persons.add(person);

  Person person2 = new Person();
  person2.setId("Id 2");
  person2.setName("Person Name 2");

  persons.add(person2);

  return new ResponseEntity(persons, HttpStatus.OK);
 }
}


Output:
[{"id":"Id 1","name":"Person Name 1"},{"id":"Id 2","name":"Person Name 2"}]







No comments:

Post a Comment