title: Spring 4 and REST Jersey 2(Annotation configuration)
body: <p>I have Spring(mvc and security) app that was configured by annotation and classes. No additional xml and such</p>
  
  <pre><code>&lt;web-app
      xmlns="http://java.sun.com/xml/ns/javaee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
      version="3.0"&gt;
  &lt;display-name&gt;Archetype Created Web Application&lt;/display-name&gt;
  &lt;servlet&gt;
  &lt;servlet-name&gt;jersey-serlvet&lt;/servlet-name&gt;
  &lt;servlet-class&gt;
    org.glassfish.jersey.servlet.ServletContainer
  &lt;/servlet-class&gt;
  &lt;init-param&gt;
    &lt;param-name&gt;jersey.config.server.provider.packages&lt;/param-name&gt;
    &lt;param-value&gt;com.nixsolutions.servicestation.service.impl&lt;/param-value&gt;
  &lt;/init-param&gt;
  &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  
  &lt;servlet-mapping&gt;
  &lt;servlet-name&gt;jersey-serlvet&lt;/servlet-name&gt;
  &lt;url-pattern&gt;/services/*&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>web.xml. Now I have task to create jersey rest web services.
  I've added such dependencies</p>
  
  <pre><code>  &lt;&lt;dependency&gt;
    &lt;groupId&gt;javax.ws.rs&lt;/groupId&gt;
    &lt;artifactId&gt;javax.ws.rs-api&lt;/artifactId&gt;
    &lt;version&gt;2.0.1&lt;/version&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
    &lt;groupId&gt;org.glassfish.jersey.ext&lt;/groupId&gt;
    &lt;artifactId&gt;jersey-spring3&lt;/artifactId&gt;
    &lt;version&gt;2.22.1&lt;/version&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
    &lt;groupId&gt;org.glassfish.jersey.core&lt;/groupId&gt;
    &lt;artifactId&gt;jersey-client&lt;/artifactId&gt;
    &lt;version&gt;2.22.1&lt;/version&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
    &lt;groupId&gt;org.glassfish.jersey.test-framework.providers&lt;/groupId&gt;
    &lt;artifactId&gt;jersey-test-framework-provider-inmemory&lt;/artifactId&gt;
    &lt;version&gt;2.22.1&lt;/version&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>Now I have fail while deployment:</p>
  
  <pre><code>No annotated classes found for specified class/package [classpath:applicationContext.xml]
  </code></pre>
  
  <p>If I remove jersey-spring3 deployment will complete successfully. But then my web-services doesn't work. As I read there is no mapping for beans without that library.
  If there any possibility to complete project without reducing Spring to version 3 and creating all those xml with mappings?</p>
  
  <pre><code>    @EnableWebMvc
      @Configuration
      @ComponentScan(basePackages = "com.nixsolutions.servicestation")
      public class AppConfig extends WebMvcConfigurerAdapter {
  
      @Override
      public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
          configurer.enable();
      }
  
      @Bean
      public InternalResourceViewResolver getInternalResourceViewResolver() {
          InternalResourceViewResolver resolver = new InternalResourceViewResolver();
          resolver.setPrefix("/WEB-INF/jsp/");
          resolver.setSuffix(".jsp");
          return resolver;
      }
      }
  
  
  
  
          @Configuration
          @EnableTransactionManagement
          @ComponentScan({"com.nixsolutions.util"})
          @PropertySource(value = { "classpath:springhiber.properties" })
          public class HiberUtil {
          @Autowired
          private Environment environment;
  
          @Bean
          public LocalSessionFactoryBean sessionFactory() {
              LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
              sessionFactory.setDataSource(dataSource());
              sessionFactory.setPackagesToScan(new String[] { "com.nixsolutions.servicestation.entity" });
              sessionFactory.setHibernateProperties(hibernateProperties());
              return sessionFactory;
          }
  
          @Bean
          public DataSource dataSource() {
              DriverManagerDataSource dataSource = new DriverManagerDataSource();
              dataSource.setDriverClassName("org.h2.Driver");
              dataSource.setUrl(environment.getRequiredProperty("JDBC_URL"));
              dataSource.setUsername("root");
              dataSource.setPassword("root");
              return dataSource;
          }
  
          private Properties hibernateProperties() {
              Properties properties = new Properties();
              properties.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
              properties.put("hibernate.show_sql", "false");
              properties.put("hibernate.format_sql", "false");
              properties.put("hibernate.hbm2ddl.auto", "create");
              properties.put("hibernate.hbm2ddl.import_files", "import.sql");
              return properties;
          }
  
          @Bean
          @Autowired
          public HibernateTransactionManager transactionManager(SessionFactory s) {
              HibernateTransactionManager txManager = new HibernateTransactionManager();
              txManager.setSessionFactory(s);
              return txManager;
          }
         }
  
  
  
      @EnableWebSecurity
      @Configuration
      @ComponentScan(value = "com.nixsolutions")
      public class SecurityConfig extends WebSecurityConfigurerAdapter {
      @Autowired
      DataSource dataSource;
  
      @Autowired
      public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
          auth
                  .jdbcAuthentication()
                  .dataSource(dataSource)
                  .usersByUsernameQuery("SELECT login AS username, password AS password, 'true' " +
                          "FROM user WHERE login = ?")
                  .authoritiesByUsernameQuery("SELECT u.login AS username, " +
                          "CASE r.role_name WHEN 'manager' THEN 'ROLE_ADMIN' " +
                          "ELSE 'ROLE_USER' END AS role FROM user u " +
                          "INNER JOIN role r ON u.role_id = r.role_id WHERE u.login = ?");
      }
  
      @Override
      protected void configure(HttpSecurity http) throws Exception {
          http
                  .authorizeRequests()
                  .antMatchers("/homepage").access("hasRole('ROLE_USER') or hasRole('ROLE_ADMIN')")
                  .antMatchers("/cars", "/clients", "/orders", "/workers").access("hasRole('ROLE_ADMIN')")
                  .and().formLogin()
                  .loginPage("/")
                  .loginProcessingUrl("/j_spring_security_check")
                  .usernameParameter("login")
                  .passwordParameter("password")
                  .defaultSuccessUrl("/workers")
                  .and().csrf()
                  .and().exceptionHandling().accessDeniedPage("/homepage");
      }
  }
  
  
      public class MyWebAppInitializer implements WebApplicationInitializer {
      @Override
      public void onStartup(ServletContext servletContext) throws ServletException {
          AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
          ctx.register(SecurityConfig.class);
          servletContext.addListener(new ContextLoaderListener(ctx));
          FilterRegistration.Dynamic securityFilter = servletContext.addFilter("springSecurityFilterChain", DelegatingFilterProxy.class);
          securityFilter.addMappingForUrlPatterns(null, false, "/*");
          ctx.setServletContext(servletContext);
          ServletRegistration.Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
          servlet.addMapping("/");
          servlet.setLoadOnStartup(1);
      }
  }
  </code></pre>