小编bar*_*kur的帖子

字符串格式的自动字段编号

我正在尝试使用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)

java

9
推荐指数
2
解决办法
6024
查看次数

即使添加了过滤器,也不会对帖子正文进行编码

我在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)

java spring spring-mvc

5
推荐指数
1
解决办法
68
查看次数

Cookie 在 Apache httpclient 4.4 中被忽略

我将 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.DEFAULTCookieSpecs.STANDARDCookieSpecs.STANDARD_STRICT,但似乎没有工作。

java apache-httpclient-4.x

2
推荐指数
1
解决办法
6400
查看次数

java中的高阶函数和闭包

以下代码模仿高阶函数和闭包.

static Function<Integer, Integer> getDoubleFunc(int multi) {
    return (val) -> {
        return val * multi;
    };
}
Run Code Online (Sandbox Code Playgroud)

但为什么它不被认为是一个?是因为这里的函数是一个接口吗?

java closures java-8

1
推荐指数
1
解决办法
389
查看次数