Solved

What does @EnableWebMvc annotation in spring do

Posted on 2016-07-27
2
154 Views
Last Modified: 2016-08-13
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
Comment
Question by:Rohit Bajaj
2 Comments
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
ID: 41732192
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now