2016-03-06 82 views
0

我是新來的彈簧。只是想知道我們是否可以驗證請求參數密鑰。 我已經使用RequestParam作爲你可以看到,例如@RequestParam(value= "sourceType")這裏value = "sourceType"是我的鑰匙,所以如果用戶輸入sourcety哪個不匹配給定的鍵,它給出400錯誤與url缺少的url。而不是給404錯誤,我希望它只給url中缺少的sourceType或無效的sourceType? 這裏是我的控制器,如何驗證彈簧中的請求參數鍵碼

@Scope("request") 
@RestController 
public class GetOperatorSeries{ 
@RequestMapping(value = "test", method = RequestMethod.GET) 
    public String getOperatorSeries(HttpServletResponse response, @RequestParam(value = "mobno") long mobno, 
      @RequestParam(value = "sourceType") String sourceType, 
      @RequestParam(value = "responseType") String responseType) 
{ 

} 

這裏是我的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" xmlns:web="http://java.sun.com/xml/ns/javaee" 
    xmlns:web1="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_2_5.xsd" 
    id="voiceBank" version="2.5"> 
    <session-config> 
     <session-timeout>60</session-timeout> 
    </session-config> 
    <servlet> 
     <servlet-name>testmemspring</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>testmemspring</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 


    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value> 
      /WEB-INF/testmemspring-servlet.xml 
      </param-value> 
    </context-param> 

這裏是我的Spring配置文件

<?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:p="http://www.springframework.org/schema/p" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:security="http://www.springframework.org/schema/security" 
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" 
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 

    <context:component-scan base-package="com.one97.org" /> 
    <context:annotation-config /> 
    <mvc:annotation-driven /> 

    <bean id="testSpring" class="com.one97.org.controller.TestSpring" 
     scope="request"> 
    </bean> 



    <bean id="internalViewResolver" 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/WEB-INF/" /> 
     <property name="suffix" value=".jsp" /> 
     <property name="order" value="1" /> 
    </bean> 




</beans> 

回答

0

你可以聲明你的要求PARAM可選。然後你可以手動檢查它是否存在。根據它你可以顯示自定義錯誤信息。例如 -

@Scope("request") 
@RestController 
public class GetOperatorSeries{ 
@RequestMapping(value = "test", method = RequestMethod.GET) 
    public String getOperatorSeries(HttpServletResponse response, @RequestParam(value = "mobno") long mobno, 
      @RequestParam(value = "sourceType", required=false) String sourceType, 
      @RequestParam(value = "responseType") String responseType, Model model) 
{ 

    if (sourceType == null) { 
    model.addAttribute("errorMessage", "Source Type is missing"); 
    return "viewName"; // Now show the error message in view. You can also add this error as flash attribute. You can also show a new error page. 


    } 

} 
+0

嗨,先生,我試過這個,但仍然得到相同的錯誤。這是輸出HTTP錯誤400 問題訪問/ TestMemSpring /測試。原因: 必需字符串參數'sourceType'不存在 –

+0

嘿,它的工作正常。其實我沒有使用required = false。謝謝您的幫助。請詳細說明這需要什麼=虛假嗎? –

+0

請求參數必需爲false意味着它不是強制性的。你可以提供或不提供。默認情況下,所需的值是true。所以如果你沒有在你的請求中提供請求參數,它會給HTTP 400一個錯誤。所以爲了使請求參數可選,你需要使用required = false。 –