我已经阅读了有关该问题的每个主题,并且我知道要在不设置@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/");
}
}
答案 0 :(得分:2)
问题是您已在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
或@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的错误