Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在springs中验证请求参数键_Java_Spring Mvc - Fatal编程技术网

Java 如何在springs中验证请求参数键

Java 如何在springs中验证请求参数键,java,spring-mvc,Java,Spring Mvc,我是春天的新手。只是想知道我们是否可以验证请求参数密钥。 我使用了RequestParam,正如你所看到的,例如@RequestParam(value=“sourceType”)这里value=“sourceType”是我的密钥,所以如果用户输入的sourcety与给定的密钥不匹配,它会给出url中缺少sourceType的400错误。与其给出404错误,不如只给出url中缺少的sourceType或无效的sourceType? 这是我的控制器 @Scope("request") @RestCo

我是春天的新手。只是想知道我们是否可以验证请求参数密钥。 我使用了
RequestParam
,正如你所看到的,例如
@RequestParam(value=“sourceType”)
这里
value=“sourceType”
是我的密钥,所以如果用户输入的
sourcety
与给定的密钥不匹配,它会给出url中缺少sourceType的
400
错误。与其给出
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>

60
testmemspring
org.springframework.web.servlet.DispatcherServlet
1.
testmemspring
/
org.springframework.web.context.ContextLoaderListener
上下文配置位置
/WEB-INF/testmemspring-servlet.xml

这是我的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>

您可以将请求参数声明为可选参数。然后您可以手动检查它是否存在。根据具体情况,您可以显示自定义错误消息。例如—

@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.


    }

}

嗨,先生,我试过这个,但仍然得到同样的错误。下面是访问/TestMemSpring/test时出现的输出HTTP错误400。原因:必需的字符串参数“sourceType”不存在。它们工作正常。实际上,我没有使用required=false。谢谢你的帮助。请详细说明这个required=false做什么?Request param required false表示它不是强制性的。你可以提供也可以不提供。默认情况下,required的值为true。因此,如果您不在请求时提供请求参数,它将给出错误HTTP400。因此,要使请求参数成为可选的,您需要使用required=false.Hi Sir,在传递此url localhost:8080/MemcachedData/test?mobno=8877546122&sourceType=##&responseType=11时。传递“#”时得到空值。这是因为#在url处有特殊含义。还有一些其他字符在url中使用时具有特殊意义,包括&。因此,如果你想使用任何特殊字符,你需要转义。要逃脱,您需要使用%26。您可以在此处看到完整的列表: