我正在尝试使用MessageFormat类在java中格式化String .String包含位置字段,但只有在我提供字段编号时才有效.例如
MessageFormat.format("{0} {1}", "Hi", "Java") 工作,但
MessageFormat.format("{} {}", "Hi", "Java") 给
Exception in thread "main" java.lang.IllegalArgumentException: can't parse argument number:
at java.text.MessageFormat.makeFormat(MessageFormat.java:1429)
at java.text.MessageFormat.applyPattern(MessageFormat.java:479)
at java.text.MessageFormat.<init>(MessageFormat.java:362)
at java.text.MessageFormat.format(MessageFormat.java:840)
at controller.App.main(App.java:11)
Caused by: java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:592)
at java.lang.Integer.parseInt(Integer.java:615)
at java.text.MessageFormat.makeFormat(MessageFormat.java:1427)
Run Code Online (Sandbox Code Playgroud)
有没有办法(任何其他类或库),它可以处理像python一样的自动和手动字段编号?
In [3]: print "{} {}".format("Hi", "Java")
Hi Java
In [4]: print "{0} {1}".format("Hi", "Java")
Hi Java
Run Code Online (Sandbox Code Playgroud) 我在web.xml中使用了CharacterEncodingFilter(第一个过滤器)
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Run Code Online (Sandbox Code Playgroud)
但是当我发出POST请求时,正文不会被编码Req Body Sent:
{
"hi": "??"
}
Run Code Online (Sandbox Code Playgroud)
但收到了
{
"hi": "??"
}
Run Code Online (Sandbox Code Playgroud) 我将 Apache http 客户端从 4.3.6 升级到 4.4 并观察到 cookie 被忽略了。知道如何让 cookie 在 4.4 中工作吗?
编辑:代码片段
CookieStore cookieStore = new BasicCookieStore();
cookieStore.addCookie(new BasicClientCookie("name", "value"));
RequestConfig config = RequestConfig.custom().setCookieSpec(CookieSpecs.DEFAULT).build();
HttpClient client = HttpClientBuilder.create()
.disableRedirectHandling()
.setDefaultRequestConfig(config)
.setDefaultCookieStore(cookieStore)
.build();
Run Code Online (Sandbox Code Playgroud)
我想CookieSpecs.DEFAULT,CookieSpecs.STANDARD和CookieSpecs.STANDARD_STRICT,但似乎没有工作。
以下代码模仿高阶函数和闭包.
static Function<Integer, Integer> getDoubleFunc(int multi) {
return (val) -> {
return val * multi;
};
}
Run Code Online (Sandbox Code Playgroud)
但为什么它不被认为是一个?是因为这里的函数是一个接口吗?