[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1550
  • Last Modified:

What does @EnableWebMvc annotation in spring do

Hi,
I am using the following class :
package net.media.springmvcexample;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;

@Configuration
@PropertySource("classpath:application.properties")
@EnableWebMvc
public class AppConfig {
    @Value("${db.user}") private String dbUser;
    @Value("${db.passwd}") private String dbPasswd;
    @Value("${db.driver}") private String dbDriver;
    @Value("${db.jdbc.url}") private String dbJdbcUrl;
    @Value("${db.min.pool.size}") private int dbMinPoolSize;
    @Value("${db.max.pool.size}") private int dbMaxPoolSize;
    @Value("${db.init.pool.size}") private int dbInitPoolSize;
    @Value("${db.max.statements}") private int dbMaxStatements;

    @Bean
    public JdbcTemplate dataSource() throws PropertyVetoException {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setUser(dbUser);
        dataSource.setPassword(dbPasswd);
        dataSource.setDriverClass(dbDriver);
        dataSource.setJdbcUrl(dbJdbcUrl);
        dataSource.setMinPoolSize(dbMinPoolSize);
        dataSource.setMaxPoolSize(dbMaxPoolSize);
        dataSource.setInitialPoolSize(dbInitPoolSize);
        dataSource.setMaxStatements(dbMaxStatements);
        return new JdbcTemplate(dataSource);
    }

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }
}

Open in new window


I noticed that if i remove @EnableWebMvc annotation some request mappings stop working. :

In my controller code i have two mappings likes :
@RequestMapping(value = "/api/users/create", method = RequestMethod.POST)
    @ResponseBody

@RequestMapping(value = "/", method = RequestMethod.GET)
    @ResponseBody

and localhost:8080/ works
But post request to /api/users/create does not work.

What could be the reason for this ?

Thanks
0
Rohit Bajaj
Asked:
Rohit Bajaj
1 Solution
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Did you check the Javadoc?

http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/config/annotation/EnableWebMvc.html

In there, it tells you that it enables some default configuration via another class...

http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.html

And in that Javadoc, it mentions all the default things that it sets up for you. Probably the reason why you are seeing some things work and some not, is because not ALL requests required these supporting classes to be available to work. I'd guess why your POST request is failing is because it would need the RequestMapping... classes plus probably at least one of the HttpMessageConverter instances that gets setup for you.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now