Java Spring 4.1.6.RELEASE:@RequestParam defaultValue和required=false don';不影响
我已经阅读了关于这个问题的每一个主题,我知道要使rest端点在不设置Java Spring 4.1.6.RELEASE:@RequestParam defaultValue和required=false don';不影响,java,spring,spring-mvc,Java,Spring,Spring Mvc,我已经阅读了关于这个问题的每一个主题,我知道要使rest端点在不设置@RequestParam的情况下可用,需要在其中设置required=false。我还了解到4.1版中仍然存在一个bug,并且该参数不会影响控制器的行为,因此defaultValue参数应该可以完成这项工作。 但这两种方法对我都不起作用:我尝试了defaultValue和required中的每一种,并且同时尝试了这两种方法,我甚至尝试了它们的不同顺序-如果我不将这些参数放在get地址中,我仍然会得到Status 400 Bad
@RequestParam
的情况下可用,需要在其中设置required=false
。我还了解到4.1版中仍然存在一个bug,并且该参数不会影响控制器的行为,因此defaultValue
参数应该可以完成这项工作。
但这两种方法对我都不起作用:我尝试了defaultValue
和required
中的每一种,并且同时尝试了这两种方法,我甚至尝试了它们的不同顺序-如果我不将这些参数放在get地址中,我仍然会得到Status 400 Bad Request
。
那么解决办法是什么呢?谢谢
我的代码如下:
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;
}
}
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>
org.springframework
弹簧芯
${org.springframework版本}
org.springframework
spring上下文
${org.springframework版本}
公用记录
公用记录
org.springframework
SpringWebMVC
${org.springframework版本}
org.springframework
春季甲虫
${org.springframework版本}
org.springframework
弹簧网
${org.springframework版本}
org.springframework
弹簧试验
${org.springframework版本}
测试
和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>
上下文类
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
org.springframework.web.context.ContextLoaderListener
上下文配置位置
com.demshin.medpro.configuration.WebAppConfiguration
springServlet
org.springframework.web.servlet.DispatcherServlet
上下文类
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
1.
springServlet
/*
和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/");
}
}
@EnableWebMvc
@配置
@组件扫描(“com”)
@导入(MySQLconfiguration.class)
公共类WebAppConfiguration扩展了WebMVCConfigureAdapter{
@豆子
公共映射Jackson2HttpMessageConverter jacksonMessageConverter(){
MappingJackson2HttpMessageConverter messageConverter=新的MappingJackson2HttpMessageConverter();
ObjectMapper mapper=新的ObjectMapper();
//注册Hibernate4Module以支持惰性对象
registerModule(新的Hibernate4Module());
messageConverter.setObjectMapper(映射器);
返回消息转换器;
}
@凌驾
public void configureMessageConverters(List问题是您在@RequestMapping
级别中显式指定了params={“param”}
,这将继承到其较低级别。这意味着这应该存在于请求中
例如:
@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;
}
这里,newparam
I声明为RequestParam
,并作为可选项提供。因此请求:
http://localhost:8080/api?param=test
将起作用。但是如果我们从请求中避免param
,它将给出400
,因为它已经在RequestMapping
中声明,所以它应该在请求中。您需要在RequestMapping
中指定request param,或者将其指定为@RequestParam。这意味着declar的正确方法e上述方法为:
更正:
@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;
}
问题的简单解决方案是从@RequestMapping
中删除params={“param”}
所以我不认为这是Spring的一个bug这是一个很棒的解释,我很感激!现在它可以按照我的需要工作了。