0

I have Spring(mvc and security) app that was configured by annotation and classes. No additional xml and such

<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">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>
  org.glassfish.jersey.servlet.ServletContainer
</servlet-class>
<init-param>
  <param-name>jersey.config.server.provider.packages</param-name>
  <param-value>com.nixsolutions.servicestation.service.impl</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>

web.xml. Now I have task to create jersey rest web services. I've added such dependencies

  <<dependency>
  <groupId>javax.ws.rs</groupId>
  <artifactId>javax.ws.rs-api</artifactId>
  <version>2.0.1</version>
</dependency>
<dependency>
  <groupId>org.glassfish.jersey.ext</groupId>
  <artifactId>jersey-spring3</artifactId>
  <version>2.22.1</version>
</dependency>
<dependency>
  <groupId>org.glassfish.jersey.core</groupId>
  <artifactId>jersey-client</artifactId>
  <version>2.22.1</version>
</dependency>
<dependency>
  <groupId>org.glassfish.jersey.test-framework.providers</groupId>
  <artifactId>jersey-test-framework-provider-inmemory</artifactId>
  <version>2.22.1</version>
</dependency>

Now I have fail while deployment:

No annotated classes found for specified class/package [classpath:applicationContext.xml]

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?

    @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);
    }
}
  • Why are all your dependencies `provided`? That's why there are no services when you remove spring You need `jersey-container-servlet`, which you have as `provided`. Unless you are deploying to Glassfish, then it doesn't make any sense for it to be `provided`. See [this post](http://stackoverflow.com/a/32357991/2587435) for why it doesn't work with with spring. – Paul Samsotha Feb 11 '16 at 13:52
  • I've edited my post, there are no 'provided' now. There is too much copy-paste in my web.xml.. – Vitaliy Rybkin Feb 11 '16 at 14:10
  • Still, the link I provided might help you solve your problem. – Paul Samsotha Feb 11 '16 at 14:14

0 Answers0