我已经阅读了有关该问题的所有主题,并且我知道要使其余端点可用而不设置@RequestParam
,需要在其中设置required=false
。我还知道4.1版中仍然存在一个错误,并且此参数不会影响控制器的行为,因此defaultValue
参数应该可以完成工作。
仍然对我不起作用:我同时尝试了defaultValue
和required
的每一个,并且同时尝试了它们的两个顺序,甚至尝试了不同的顺序-如果我不放置它们,我仍然会得到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>
和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>
和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/");
}
}
最佳答案
问题是您在params = { "param"}
级别中明确指定了@RequestMapping
,它将被继承到其较低级别。这意味着它应该在请求中。
对于有问题的示例:
@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
声明为RequestParam
并指定为可选。http://localhost:8080/api?param=test
将工作。但是如果我们避免在请求中使用
param
,它将给出400
,因为它已经在RequestMapping
中声明了,所以它应该在请求中。您需要在RequestMapping
或as中指定请求参数@RequestParam。这意味着声明上述方法的正确方法是:已更正:
@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;
}
简要解决您的问题的方法是从
params = { "param"}
中删除@RequestMapping
所以我不认为这是Spring的错误
关于java - Spring 4.1.6.RELEASE:@RequestParam defaultValue和required = false不影响,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34081110/