Spring boot How to add prefix "/api" to certain urls, but not on "/" "/login"

Viewed 153

springboot 2.2.4 version

so i was trying to make prefix to every controller on my application

"/api"

i have done by following code

//DispatcherServletCustomConfiguration.java
@Configuration
public class DispatcherServletCustomConfiguration {

    @Bean
    public DispatcherServlet dispatcherServlet() {
        return new DispatcherServlet();
    }

    @Bean
    public DispatcherServletRegistrationBean dispatcherServletRegistration() {
        DispatcherServletRegistrationBean registration = new DispatcherServletRegistrationBean(
                dispatcherServlet(), "/api/");
        registration.setName(DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME);
        return registration;
    }
}

but i want to exclude certain urls that returns static resources

such as "/" "/login" "/404page"

those url need to return index.html

however by adding those prefix, "index.html" is mapped to /api/

how can i distinguish url that return static resources(image, html, css)

and api calls that returns json to add prefix

ex) there is too many controller to add requestMapping for each controller

1 Answers

1st solution:

Remove the DispatcherServletRegistrationBean configuration and configure nothing at servlet level.

Use @RequestMapping("/api") on all the controllers at class level. Then you can achieve that the "/api" will be appended at the initial path for all requests and also the "/", "/login" paths can also be called succesfully.

2nd solution:

add url mappings in the config like below:

package com.test.sampleproject;

import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.DispatcherServlet;

@Configuration
public class AppConfig {

    @Bean
    public DispatcherServlet dispatcherServlet() {
        return new DispatcherServlet();
    }

    @SuppressWarnings("rawtypes")
    @Bean
    public ServletRegistrationBean axisServletRegistrationBean() {
        @SuppressWarnings("unchecked")
        ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet(), "/*");
        registration.addUrlMappings("/api/*");
        return registration;
    }
}

Controller class:

package com.test.sampleproject.controller;

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


@RestController
public class RetryController {



    @GetMapping("/test1")
    public String sampleApi1() {
        
        return "test1 api called";
    }

    @GetMapping("/")
    public String sampleApi2() {
        return "index";
    }
}

The following url's are valid from this config:

  • localhost:8080/ ---> will hit the "/" path method in RetryConroller

  • localhost:8080/api/test1 ---> will hit the "test1" path method in RetryConroller

  • localhost:8080/test1 ---> will hit the "test1" path method in RetryConroller

Related