Home > Software design >  Spring MVC can't find mappings
Spring MVC can't find mappings

Time:11-03

I am trying to get my OrderController to work, but MVC can't seem to find it. Does anyone know why this happens?

Initializer class. The getServletMapping method keeps notifying me about Not annotated method overrides method annotated with @NonNullApi

package configs;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class Initializer extends AbstractAnnotationConfigDispatcherServletInitializer {

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

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

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/api/*" };
    }
}

The config class.

package configs;

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.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import javax.sql.DataSource;

@EnableWebMvc
@Configuration
@ComponentScan(basePackages = {"model"})
@PropertySource("classpath:/application.properties")
public class Config {

    @Bean
    public JdbcTemplate getTemplate(DataSource ds) {
        return new JdbcTemplate(ds);
    }

    @Bean
    public DataSource dataSource(Environment env) {
        DriverManagerDataSource ds = new DriverManagerDataSource();
        ds.setDriverClassName("org.hsqldb.jdbcDriver");
        ds.setUrl(env.getProperty("hsql.url"));

        var populator = new ResourceDatabasePopulator(
                new ClassPathResource("schema.sql"),
                new ClassPathResource("data.sql")
        );

        DatabasePopulatorUtils.execute(populator, ds);

        return ds;
    }
}

Then the controller.

package controllers;

import model.Order;
import model.OrderDAO;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class OrderController {

    private OrderDAO orderdao;

    public OrderController(OrderDAO orderDAO) {
        this.orderdao = orderDAO;
    }

    @PostMapping("orders")
    @ResponseStatus(HttpStatus.CREATED)
    public Order saveOrder(@RequestBody Order order) {
        return orderdao.addOrder(order);
    }

    @GetMapping("orders/{id}")
    public Order getOrderById(@PathVariable Long id) {
        return orderdao.getOrderById(id);
    }

    @GetMapping("orders")
    public List<Order> getOrders() {
        return orderdao.getAllOrders();
    }

    @DeleteMapping("orders/{id}")
    public void deleteOrderById(@PathVariable Long id) {
        orderdao.deleteOrderById(id);
    }
}

Everything seems fine, I can't find the issue.

CodePudding user response:

Your controller registers the endpoints at /orders/*, but in getServletMappings you specify the /api/*. You should add /api prefix to your controller, like this

@RestController
@RequestMapping("api")
public class OrderController {
...

CodePudding user response:

Since the OrderController is in the controllers package, I forgot to add the package in the configs componentScan parameters.

  • Related