How to change Spring MVC's behavior in handling url 'dot' character

前端 未结 6 627
耶瑟儿~
耶瑟儿~ 2020-12-06 11:40

I\'m trying to migrate a web project off from Jersey to Spring MVC 3.0. The process was really straightforward up to the moment when I started to migrate the controllers sup

相关标签:
6条回答
  • 2020-12-06 12:10

    In order for

    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
    

    to work you have to remove/comment out the

    <mvc:annotation-driven />
    <mvc:default-servlet-handler />
    

    and any other mvc: configurations defined as they override your custom bean declarations.

    0 讨论(0)
  • 2020-12-06 12:12

    Try this:

    <bean
            class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
            <property name="useDefaultSuffixPattern" value="false" />
        </bean>
    

    This should make it so that Spring won't try to parse extensions.

    See also: Spring MVC @PathVariable getting truncated

    And: Spring Documentation

    Potential config files:

    web.xml (where I set the servlet stuff)

    <servlet>
        <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                /WEB-INF/spring/*.xml,classpath*:applicationContext.xml,classpath*:restApplicationContext.xml
            </param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>
    

    And then in WEB/INF/spring/mvc-config.xml I have something like this:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    
        <bean
            class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
            <property name="useDefaultSuffixPattern" value="false" />
        </bean>
    </beans>
    
    0 讨论(0)
  • 2020-12-06 12:13

    This was a bitch. A guy on our team here got it to work over the weekend. We were going to go with underscore but he got it. He removed @Controller from the controller class attributes and left @RequestMapping blank for the class. On the public methods for Http get and put he added "/2.0/" to the @RequestMapping. Example below.

    @RequestMapping
    public class EndpointController {
    
    @RequestMapping(value="/2.0/endpoint", method = RequestMethod.POST,
    consumes=MediaType.APPLICATION_JSON_VALUE, produces=MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<EndpointView> create( @Valid @RequestBody EndpointView endpointParams, HttpServletRequest request )
    

    Good luck everyone. This problem sucked.

    0 讨论(0)
  • 2020-12-06 12:15

    Another possibility which may be easier in some circumstances is to add a trailing / to the end of the URL.

    So your URL would be /myApp/resources/create/root.subFolder1/

    This works in my app using Spring MVC 3.1.

    0 讨论(0)
  • 2020-12-06 12:21

    The above answer should correct, but be sure to remove <mvc:annotation-driven/> from your XML configuration, otherwise the bean definition is overruled by the one part of the annotation-driven tag.

    See the java docs and the source code for all the configuration you miss when this convenience tag is removed.

    0 讨论(0)
  • 2020-12-06 12:36

    I did almost the same as Robert Beltran but I left @Controller annotation for the class. Important is to put "/1.0/" to the @RequestMapping on the methods, not in class annotation.
    My example:

    @Controller
    @RequestMapping("")
    public class ClientApi {
    
    private final String API_PREFIX = "/api/1.0/client";
    
    @RequestMapping(value = API_PREFIX + "/{clientId}", method = RequestMethod.GET)
    @ResponseBody
    public ClientDetailsImpl get(@PathVariable String clientId) {
        // my code
    }
    }
    
    0 讨论(0)
提交回复
热议问题