Home > Net >  Spring MVC make a rest controller without suffix in URL. My other controllers need to have suffix
Spring MVC make a rest controller without suffix in URL. My other controllers need to have suffix

Time:01-13

I am running a Spring Project which is a combination of Spring MVC and Spring boot. Its configuration has set all the controllers must need to use .html in the URL suffix. Now I need to connect with a third party that shared a predefined URL that I have to make where URL does not have any suffixes. My system URL https://mysystem.com/api/urls.html I need to have https://mysystem.com/thrid_party_string I am facing trouble configuring. Both at the same time. how can I manage?

Note: I cannot change existing controllers since they are already in us for many services.

My web.xml file

<?xml version="1.0" encoding="UTF-8"?>


<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>test</display-name>  
    <context-param>
        <param-name>webAppRootKey</param-name>
        <param-value>webapp.test</param-value>
    </context-param>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContextService.xml</param-value>
    </context-param>

    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>classpath:log4j.properties</param-value>
    </context-param>
    <context-param>
        <param-name>log4jExposeWebAppRoot</param-name>
        <param-value>false</param-value>
    </context-param>
    
    

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>


<!-- filter -->
<filter>
     <filter-name>Set Character Encoding</filter-name> 
     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 
     <init-param> 
            <param-name>encoding</param-name> 
            <param-value>UTF-8</param-value> 
    </init-param> 
    <init-param> 
            <param-name>forceEncoding</param-name> 
            <param-value>true</param-value> 
    </init-param> 
</filter>
    <!-- filter-mapping -->
<filter-mapping>
        <filter-name>Set Character Encoding</filter-name>
        <url-pattern>*.asx</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>Set Character Encoding</filter-name>
        <url-pattern>*.m3u8</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>Set Character Encoding</filter-name>
        <url-pattern>*.html</url-pattern>
    </filter-mapping>
 <filter-mapping> 
    <filter-name>Set Character Encoding</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping>



   <filter>
      <filter-name>cors</filter-name>
      <filter-class>some.com.CORSFilter</filter-class>
    </filter>
      
    <filter-mapping>
      <filter-name>cors</filter-name>
      <url-pattern>/*</url-pattern>
    </filter-mapping>

    
    <!-- Standard Action Servlet Configuration -->
    <servlet>
        <servlet-name>spring-mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:mvc-dispatcher-servlet.xml</param-value>
        </init-param>
        <load-on-startup>3</load-on-startup>
    </servlet>

    <!-- Standard Action Servlet Mapping -->
    <servlet-mapping>
        <servlet-name>spring-mvc-dispatcher</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>

    <error-page>       
       <error-code>404</error-code>
       <location>/general_error.html</location>
    </error-page>
    
     <error-page>       
       <error-code>500</error-code>
       <location>/general_error_500.html</location>
    </error-page>


</web-app>

CodePudding user response:

By using spel(Spring expression language) you can set prefix for each controller

 @Controller
 @RequestMapping(path = "${apiPrefix}/users")
 public class UserController {

  } 

Then, we simply specify the property value in our application.properties:

 apiPrefix=/api

for more information you can see its

documentatihttps://www.baeldung.com/spring-boot-controllers-add-prefixon

CodePudding user response:

Normally if you're the one providing the service, the caller needs to adjust to your URL patterns, not the other way around.

That said... it appears recent servlet specs can have more than one url-pattern. If the desired REST URLs don't have a common pattern of their own, like /api/xxx, you might have to bind the dispatcher to / and expect a lot of URLs that don't match the REST ones or *.html to just produce internal 404 errors

  • Related