Solved

What does @EnableWebMvc annotation in spring do

Posted on 2016-07-27
2
732 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
bitbucket vs gitbucket 3 129
JUnit 4 @Before and @BeforeClass differences 3 85
web services creation SOAP vs REST 5 104
eclipse with Angularjs2.0 3 72
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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.

739 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