控制器@RequestMapping错误URL

时间:2016-02-24 11:33:28

标签: java spring-mvc methods request-mapping

我的应用程序不能处理请求映射,请求总是使用相同的方法handleRequest,控制器是RestController.java,当路径为/ REST2 / *时,URL重定向到它,它运行良好但总是用同样的方法。但方法"更新"当我向locahost发送请求时,它永远不会工作:9080 / myapp / REST2 / rrr,如果我更改了URL,即localhost:9080 / myapp / REST2 / XXX,请求也将处理handleRequest方法。

RequestMapping运行不好,我尝试过其他解决方案但是没有工作..

你有什么想法吗?

Restcontroller.java

)$

弹簧servlet.xml中

public class RestController implements Controller{
private Configuration config;
private static String[] requestHeaders = {"accept", "pragma"};
private static String[] responseHeaders = {};    

public void setConfig(Configuration config)
{
    this.config = config;
}


@RequestMapping(value="/rrr") public ModelAndView update(HttpServletRequest request, HttpServletResponse response) throws Exception 
     {

    String asset_name = request.getParameter("assetid");
     response.setContentType("text/plain");  // Set content type of the response so that jQuery knows what it can expect.
     response.setCharacterEncoding("UTF-8"); // You want world domination, huh?

     String values = "Working"+ asset_name;
     response.setContentLength(values.length());

     PrintWriter out = response.getWriter();

    out.println(values);
    return null;

}

@RequestMapping(value ="/home")public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception 
{
    Arrays.sort( requestHeaders );
    Arrays.sort( responseHeaders );
    String values;

    String asset_name = request.getParameter("assetid");
    ...
   response.setContentLength(values.length());
    //
    PrintWriter out = response.getWriter();

    out.println(values);


    return null;
}}

的web.xml

    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-2.5.xsd"> 

  <!-- View resolver. Prepends prefix /WEB-INF/jsp and suffix .jsp to view names. -->
  <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
  </bean>

  <!-- Always use full path when mapping URLs to bean names. -->
  <bean id="handlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
    <property name="alwaysUseFullPath" value="true" />
  </bean>

  <!-- Home page. -->
  <bean name="/home.app" class="com.fatwire.wem.sample.HomeController">
    <property name="config" ref="config" />
  </bean>  
  <!-- Installation page. -->
  <bean name="/install.app" class="com.fatwire.wem.sample.InstallController">
    <property name="config" ref="config" />
  </bean>
  <!-- Layout page. -->
  <bean name="/layout.app" class="com.fatwire.wem.sample.LayoutController" />  
  <!-- REST proxy page. -->
  <bean name="/REST/**" class="com.fatwire.wem.sample.ProxyController">
    <property name="config" ref="config" />
  </bean>
    <!-- REST proxy page. -->
  <bean name="/REST2/**" class="com.fatwire.wem.sample.RestController">
    <property name="config" ref="config" />
  </bean>


</beans>

请求AJAX

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

<web-app version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
         http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >

  <!-- Bootstrap Spring configuration to be used by both SSO and MVC -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>  

  <!-- WEM SSO Listener -->
  <listener>
    <listener-class>com.fatwire.wem.sso.SSOListener</listener-class>
  </listener>       

  <!-- Configure Spring MVC -->
  <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>*.app</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/REST/*</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/REST2/*</url-pattern>
  </servlet-mapping>


  <!-- WEM SOO Filter -->
  <filter>
    <filter-name>WEM SSO Filter</filter-name>
    <filter-class>com.fatwire.wem.sso.SSOFilter</filter-class>
  </filter>  
  <filter-mapping>
    <filter-name>WEM SSO Filter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

</web-app>

1 个答案:

答案 0 :(得分:0)

尝试将@Controller@RequestMapping注释添加到RestController类,如下所示:

  @Controller
  @RequestMapping(value="/REST2")
  public class RestController implements Controller{

并添加method=RequestMethod.POST这样的@RequestMapping方法:

  @RequestMapping(value ="/home", method=RequestMethod.POST)
  ...
  @RequestMapping(value ="/rrr", method=RequestMethod.POST)