user2455862
user2455862

Reputation: 676

Request mapping - Spring 4 - doesn't work

I have a very simple problem. When in my .jsp files I have a link to **/registration the method viewRegistration is executed and everything is working fine. If I have a link to **/registration/getTags?tagName=blahblah page is not found. I have no idea why, because I think my requestMapping annotation looks correct... I would be very grateful for your help!

CONTROLLER:

@Controller
@RequestMapping(value = "/registration")
public class HelloController {

    final static Logger logger = Logger.getLogger(HelloController.class);

    @RequestMapping(method = RequestMethod.GET)
    public String viewRegistration(Map<String, Object> model, HttpSession session) {
        ...
   }

@RequestMapping(value = "/getTags", method = RequestMethod.GET)
    public @ResponseBody
    List<Tag> getTags(@RequestParam String tagName) {

        ....

    }
}

WEB.XML:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" 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>aa</display-name>
  <servlet>
    <servlet-name>xxx</servlet-name>
    <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>xxx</servlet-name>
    <url-pattern>/registration/*</url-pattern>
  </servlet-mapping>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/xxx-servlet.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
</web-app>

xxx-servlet.xml :

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd  
      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <context:component-scan base-package="movies.controller" />
    <context:property-placeholder location="/WEB-INF/properties/website.properties" />
    <context:component-scan base-package="com" />
    <context:annotation-config />
    <mvc:annotation-driven />

    <bean class="org.springframework.web.servlet.view.tiles3.TilesConfigurer"
        id="tilesConfigurer">
        <property name="definitions">
            <list>
                <value>/WEB-INF/tile/tilesJsp.xml</value>
            </list>
        </property>
    </bean>
    <mvc:resources mapping="/resources/**" location="/resources/" />
    <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver"
        id="viewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.tiles3.TilesView" />
    </bean>


    <bean id="messageSource"
        class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="website" />
    </bean>


</beans>

EDIT EDIT EDIT: I even tried sth more simple:

@RequestMapping(value = "/getTags")
    @ResponseBody
    public List<Tag> getTags() {
        String tagName="";
        return simulateSearchResult(tagName);

    }

but still /registration/getTags does not work..., page not found.

Upvotes: 4

Views: 8474

Answers (8)

Abhishek Anand
Abhishek Anand

Reputation: 1992

You have servlet url mapping at /registration/*

In class you have mapping at /registration

In method you have mapping at /getTags

So, your url is /registration/registration/getTags?name=blahblah

Upvotes: 1

vine
vine

Reputation: 886

I'm suspecting this is related to the view resolver configuration, you are using

org.springframework.web.servlet.view.UrlBasedViewResolver

You only have one view resolver which returns jsp pages. And in your method you are trying to return a List object which I believe you want to be serialized as JSON in http body.

To return a json response you can try to use @JSONView

Spring @JsonView

and add another viewResolver like org.springframework.web.servlet.view.json.MappingJacksonJsonView on the xml config, such as illustrated by this blog

multiple view resolvers

Upvotes: 1

G555
G555

Reputation: 105

Update your url to

/registration/getTags/{blahblah}

and in controller -

@RequestMapping(value = "/getTags/{blahBlah}")
public @ResponseBody List<Tag> getTags(@PathVariable String blahBlah) {
    String tagName="";
    return simulateSearchResult(tagName);

}

Upvotes: 1

KayV
KayV

Reputation: 13855

Use the requestParam as following:

List<Tag> getTags(@RequestParam("tagName") String tagName)

Upvotes: 1

Rafik BELDI
Rafik BELDI

Reputation: 4158

the servlet url mapping is as follows :

 <servlet-mapping>
    <servlet-name>xxx</servlet-name>
    <url-pattern>/registration/*</url-pattern>
  </servlet-mapping>

which means that all urls that begin with /registration/* will be handled by the servlet, what comes after it , it will be handled by the controller. So if you configure your controller with a @RequestMapping(value="/otherURL") it will serve the following Url :

http://localhost:xxxx/<appname>/registration/otherURL

And in this case in order to access the right method you should either:

  • @RequestMapping("/registration) from the controller, cause it is already mapped on the servlet level, and calling :

    http://localhost:xxxx/<appname>/registration/getTags?tagName=blahblah will work correctly. OR:

  • Call this URL : http://localhost:xxxx/<appname>/registration/registration/getTags?tagName=blahblah

Upvotes: 5

Nick Cromwell
Nick Cromwell

Reputation: 254

Your mapping should be fine. However, I believe you're running into this because you're requesting "/registration/getTags?name=blahblah" and yet your handler's parameter is "tagName". Try changing your request to "/registration/getTags?tagName=blahblah".

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-requestparam

Upvotes: 3

Master Slave
Master Slave

Reputation: 28569

As Nick Cromwell mentioned the issue with your mapping is the mismatch between the name of the parameter in your request and in your mapping. So either change in the request as he mentions, rename the argument in the handler method, or use the value attribute of the @RequestParam annotation, e.g.

List<Tag> getTags(@RequestParam(value="name") String tagName)

Upvotes: 1

Predrag Maric
Predrag Maric

Reputation: 24433

If you want to handle requests to **/registration/getTags, change your controller method mapping to

@RequestMapping(value = "**/registration/getTags", method = RequestMethod.GET)

Upvotes: 3

Related Questions