gpt4 book ai didi

java - Spring 4.1.6.RELEASE : @RequestParam defaultValue and required=false don't affect

转载 作者:行者123 更新时间:2023-12-02 04:07:55 25 4
gpt4 key购买 nike

我已经阅读了有关该问题的每个主题,并且我知道要在不设置 @RequestParam 的情况下使其余端点可用,需要在其中设置 required=false。我还了解到 4.1 版本中仍然存在一个错误,并且此参数不会影响 Controller 的行为,因此 defaultValue 参数应该可以完成这项工作。但仍然对我不起作用:我尝试了 defaultValuerequired,并且同时尝试了它们,我什至尝试了它们的不同顺序 - 我仍然得到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/");
}
}

最佳答案

问题是您在 @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我声明为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;
}

解决您的问题的简单方法是从 @RequestMapping 中删除 params = { "param"}

所以我不认为这是 Spring 的错误

关于java - Spring 4.1.6.RELEASE : @RequestParam defaultValue and required=false don't affect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34081110/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com