[英]Spring 4.1.6.RELEASE: @RequestParam defaultValue and required=false don't affect
I've read every topic about the issue and I got to know that to make the rest endpoint available without setting @RequestParam
one needs to set in it required=false
. 我已经阅读了有关该问题的所有主题,并且我知道要使其余端点可用而不设置
@RequestParam
需要在其中设置required=false
。 I also got to know that there is a bug still available in version 4.1, and this parameter doesn't affect the behaviour of a controller, so defaultValue
parameter should do the job. 我还知道4.1版中仍然存在一个错误,并且此参数不会影响控制器的行为,因此
defaultValue
参数应该可以完成此工作。 And still neither works for me: I tried each of defaultValue
and required
, and both of them at the same time, and I even tried different order of them - I still get Status 400 Bad Request
if I don't put those parameters in the get address. 仍然对我都
defaultValue
:我同时尝试了defaultValue
和required
,并且同时尝试了它们的两个顺序,甚至尝试了不同的顺序-如果我不将这些参数放在参数中,我仍然会收到Status 400 Bad Request
获取地址。 So what's the solution? 那么解决方案是什么? Thanks.
谢谢。 My code is below:
我的代码如下:
public class Controller {
@ResponseBody
@RequestMapping(value = "/api/", params = { "param"}, method = RequestMethod.GET, produces = { "application/json" })
public Results show(HttpServletRequest request, HttpServletResponse response,
@RequestParam(value = "param", defaultValue = "all", required=false)) {
return null;
}
}
Part of pom.xml: pom.xml的一部分:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
<exclusions>
<!-- Exclude Commons Logging in favor of SLF4j -->
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework-version}</version>
<scope>test</scope>
</dependency>
and web.xml: 和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_2_5.xsd"
version="2.5">
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.demshin.medpro.configuration.WebAppConfiguration</param-value>
</context-param>
<servlet>
<servlet-name>springServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
and javaconfig: 和javaconfig:
@EnableWebMvc
@Configuration
@ComponentScan("com")
@Import(MySQLconfiguration.class)
public class WebAppConfiguration extends WebMvcConfigurerAdapter {
@Bean
public MappingJackson2HttpMessageConverter jacksonMessageConverter() {
MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
ObjectMapper mapper = new ObjectMapper();
// Registering Hibernate4Module to support lazy objects
mapper.registerModule(new Hibernate4Module());
messageConverter.setObjectMapper(mapper);
return messageConverter;
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
// Here we add our custom-configured HttpMessageConverter
converters.add(jacksonMessageConverter());
super.configureMessageConverters(converters);
}
@Bean
@Description("Thymeleaf template resolver serving HTML 5")
public ServletContextTemplateResolver templateResolver() {
ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
templateResolver.setPrefix("/WEB-INF/html/");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode("HTML5");
return templateResolver;
}
@Bean
@Description("Thymeleaf template engine with Spring integration")
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver());
return templateEngine;
}
@Bean
@Description("Thymeleaf view resolver")
public ThymeleafViewResolver viewResolver() {
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine());
viewResolver.setContentType("text/html; charset=UTF-8");
return viewResolver;
}
@Bean
@Description("Spring message resolver")
public ResourceBundleMessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("/WEB-INF/i18n/messages");
return messageSource;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/WEB-INF/resources/");
}
}
The problem is you have specified params = { "param"}
explicitly in the @RequestMapping
level, which will be inherited to its lower levels. 问题是您在
@RequestMapping
级别中明确指定了params = { "param"}
,该参数将继承到其较低级别。 That means this should be there in the request. 这意味着它应该在请求中。
For problematic example: 对于有问题的示例:
@ResponseBody
@RequestMapping(value = "/api", params = { "param"},method = RequestMethod.GET, produces = { "application/json" })
public String show(@RequestParam(value = "param", defaultValue = "all", required=false) String param, @RequestParam(value = "newparam", defaultValue = "all", required=false) String newparam) {
return param+newparam;
}
Here the newparam
I declared as RequestParam
and given as optional.So the request: 我在这里将
newparam
声明为RequestParam
并作为可选参数给出。
http://localhost:8080/api?param=test
will work. 将工作。 But if we avoid
param
from the request it will give 400
, since which is declared already in the RequestMapping
, so it should be there in the request. 但是,如果我们避免请求中的
param
,它将给出400
,因为它已经在RequestMapping
声明了,所以它应该在请求中。 You need to specify request param either in RequestMapping
or as @RequestParam . 您需要在
RequestMapping
或@RequestParam中指定请求参数 。 That means the correct way to declare above method would be: 这意味着声明上述方法的正确方法是:
Corrected: 已更正:
@ResponseBody
@RequestMapping(value = "/api", params = { "param"},method = RequestMethod.GET, produces = { "application/json" })
public String show(String param, @RequestParam(value = "newparam", defaultValue = "all", required=false) String newparam) {
return param+newparam;
}
In brief solution to your problem is remove params = { "param"}
from @RequestMapping
简要解决您的问题的方法是从
@RequestMapping
删除params = { "param"}
So I dont think it is a bug of Spring 所以我不认为这是Spring的错误
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.