samedi 3 février 2018

Spring Project not creating bean

I'm trying to develop a Spring MVC project but after running it on tomcat I'm getting 404 after hitting: http://localhost:8080/magicart-service/product I injected static code in Controller bean to see if its getting created but I don't see any output in the console. Here're all my classes:

AppConfig.class:

package com.magicart.poc.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.magicart.poc")
public class AppConfig {

}

AppInitializer.java

package com.magicart.poc.config;
import org.springframework.core.annotation.Order;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
import javax.servlet.Filter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

@Order(1)
public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer implements WebApplicationInitializer{

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[] {AppConfig.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    protected String[] getServletMappings() {
        // TODO Auto-generated method stub
        System.out.println("******Servlet Amppings*********");
        return new String[] {"/"};
    }

    protected Filter[] getServletFilters() {
        return null;
    }

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        // TODO Auto-generated method stub
        super.onStartup(servletContext);
    }
}

ProductController.java

package com.magicart.poc.controller;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.magicart.poc.service.ProductService;

@RestController
@RequestMapping(path="/product")
public class ProductController {


    @Autowired
    ProductService productService;

    @GetMapping
    public String fetchAllProducts(HttpServletResponse response) {
        System.out.println("a");
        return productService.fetchAllProducts();
    }

    static {
        System.out.println("********Controller bean****************");
    }

}

ProductService.java

package com.magicart.poc.service;

public interface ProductService {

    String fetchAllProducts();
}

ProductServiceImpl.java

package com.magicart.poc.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.magicart.poc.dao.ProductDAO;
import com.magicart.poc.service.ProductService;

@Service
public class ProductServiceImpl implements ProductService{


    @Autowired
    ProductDAO productDAO;

    @Override
    public String fetchAllProducts() {
        // TODO Auto-generated method stub
        return productDAO.fetchAllProducts();
    }
}

ProductDAO.java

package com.magicart.poc.dao;

public interface ProductDAO {

    String fetchAllProducts();
}

ProductDAOImpl.java

package com.magicart.poc.dao.impl;

import org.springframework.stereotype.Repository;

import com.magicart.poc.dao.ProductDAO;

@Repository
public class ProductDAOImpl implements ProductDAO{

    @Override
    public String fetchAllProducts() {
        return "ProductList";
    }
}

Aucun commentaire:

Enregistrer un commentaire