gpt4 book ai didi

java - 在 Spring 中,@RequestParameter 注释不接受 defaultValue 参数的 boolean 值,它只适用于字符串 URL 请求参数

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:43:36 34 4
gpt4 key购买 nike

这个问题与我在下面定义的 Spring MVC 方法有关:

writeToRead 参数的值应该是 true 或 false。我希望调用我的服务的最终用户将以下请求参数附加到 url:localhost:8080/index/someendpoint/sometype:?writeToRead=true 否则它应该默认为如果用户没有将参数附加到 url 字符串的末尾,则为 false。问题是,defaultValue=false@RequestParameter 注释中似乎不被接受。看起来它只接受字符串类型而不是我正在使用的 boolean 类型。我可以设置 defaultValue="false"但实际上,这不是 boolean 值。这是一个字符串。我应该如何处理这个问题?由于方法重载定义冲突,我无法使用字符串替代项(如“Y”或“N”)来解决此问题。我怎样才能用 boolean 值做到这一点?提前致谢!

@RequestMapping(value="/index/{endpoint}/{type}", method=RequestMethod.POST, consumes=kCompressed, produces=kProducesType)
@ResponseBody
public String indexData(@PathVariable(value="endpoint") String endpoint, @PathVariable(value="type") String type,
@RequestParam(value="writeToRead", defaultValue=false) boolean writeToRead,
@RequestBody byte[] body, HttpServletRequest request) throws Exception {
logger.debug("In indexData for endpoint " + endpoint);

String bodyStr = decompress(body);
return indexController.indexData(endpoint, type, bodyStr, writeToRead, getSecurityContextProvider(request));
}

最佳答案

声明为

@RequestParam(value="writeToRead", defaultValue="false")

是合适的做法。请求参数是字符串,没有别的。您可以解析它们并为它们提供您想要或需要的语义(例如 boolean 值),但在基础上,它们只是字符串。

关于java - 在 Spring 中,@RequestParameter 注释不接受 defaultValue 参数的 boolean 值,它只适用于字符串 URL 请求参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21116902/

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