小编hol*_*s83的帖子

使用 JsonConverter 反序列化 JSON 字典值

我使用 Json.NET 库来反序列化 JSON。对于抽象类,Foo我有一个自定义的JsonConverter. 这是我使用它的方式:

[JsonConverter(typeof(FooJsonConverter))]
public Foo MyFoo { get; set; }
Run Code Online (Sandbox Code Playgroud)

到现在为止还挺好。当我在字典中使用 Foo 类时会出现问题。这是我的尝试:

[JsonDictionary(ItemConverterType = typeof(FooJsonConverter))]
public Dictionary<string, Foo> MyFooDictionary { get; set; }
Run Code Online (Sandbox Code Playgroud)

但是上面给出了错误:

属性“JsonDictionary”在此声明类型上无效。它仅对“类,接口”声明有效。

如何为字典值指定转换器?

c# json json.net

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

不区分大小写的排序集 - 使用不同的大小写保持相同的字符串

今天我有一个不区分大小写的排序Set如下:

Set<String> set = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
set.add("foo");
set.add("FOO");
set.add("bar");
System.out.println(set.toString());
Run Code Online (Sandbox Code Playgroud)

这个输出是:

[bar, foo]
Run Code Online (Sandbox Code Playgroud)

但我真正想要的是:

[bar, FOO, foo]
Run Code Online (Sandbox Code Playgroud)

也就是说,我希望对集合的排序不区分大小写,但我希望能够在集合中使用具有不同情况的相同字符串(如"foo"和"FOO"),而不丢弃最后一个.

我知道我可以排序List,但在我的情况下,我需要一个Set.

在Java中有这样一种巧妙的方法吗?

java sorting set case-insensitive

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

Python字符串转换为Java字节[]

据我了解,Python 2字符串(类型str)不过是字节序列。如何将这样的字符串显式转换为Java字节数组?

天真的尝试不起作用:

from jarray import array
myStr = 'some str object'
myJavaArr = array(myStr, 'b') # TypeError: Type not compatible with array type
Run Code Online (Sandbox Code Playgroud)

我这样做的原因是,当Jython隐式将Python String转换为Java代码时,它错误地将其转换为Java String,因为它不知道的编码str

jython

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

Type.GetProperties()在Release中不起作用

我试图FontWeights使用反射在C#中获取类的属性.

var properties = typeof(FontWeights).GetProperties();
var dialog = new MessageDialog("Number of weights: " + properties.Length);
await dialog.ShowAsync();
Run Code Online (Sandbox Code Playgroud)

使用Debug配置构建时,上面的工作正常.但是,使用Release时,找不到任何属性.

为什么会这样?有办法解决吗?

这是一个UWP应用程序.

c# system.reflection win-universal-app

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

使用自动装配的 MockHttpServletRequest 进行的多次测试不起作用?

@Autowired MockHttpServletRequest在一些 Spring 测试中使用了 。TestNG用作测试框架。如果我在课堂上只有一种测试方法,那么效果很好。但是,如果有多个测试方法,则只有第一个运行测试使用我的 MockHttpServletRequest。让我用一个例子来说明:

@WebAppConfiguration
@ContextConfiguration({"classpath:applicationContext.xml"})
public class FooTest extends AbstractTestNGSpringContextTests {

    @Autowired
    private MockHttpServletRequest servletRequest;

    @Test
    public void test1() {
        assertEquals(((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest(), servletRequest);
    }

    @Test
    public void test2() {
        assertEquals(((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest(), servletRequest);
    }

}
Run Code Online (Sandbox Code Playgroud)

在此示例中,test1()通过了,但test2()失败了!如果单独运行测试方法,它们都会通过。如果一起运行,为什么一项测试会失败?

我尝试深入研究代码,在测试方法运行后似乎会重置请求属性,但我没有找到将其关闭的方法。我的 Spring 版本是 3.2.8.RELEASE。

testng spring spring-test

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

spring security login始终被拒绝访问

我正在尝试学习Spring安全性,所以我已经下载了一些示例项目,然后我尝试将该解决方案实现到我的项目中.但是当我尝试提交登录表单时,我总是得到403页面,这是在applicationContext-security中定义的.XML.但我预计无效用户名/密码的'authentication-failure-url'或正确用户名/密码的'default-target-url'而不是'access-denied-handler'/ forbidden(我的403页).如果有经验丰富的人可以帮助我,我会非常感激.应用程序的security.xml

<security:http security="none" pattern="/public/**"/>
<security:http security="none" pattern="/login*"/>
<security:http security="none" pattern="/maxSessionError*"/>
<security:http security="none" pattern="/forbidden*"/>
<security:http use-expressions="true">
    <security:intercept-url pattern="/**" access="isAuthenticated()"/>
    <security:form-login login-page="/login"
                         default-target-url="/home"
                         authentication-failure-url="/login"
                         authentication-success-handler-ref="loginSuccessHandler"
    />
    <security:logout  invalidate-session="true"  delete-cookies="true" success-handler-ref="logoutSuccessHandler" />
    <security:access-denied-handler error-page="/forbidden"/>
    <security:session-management session-fixation-protection="newSession" >
        <security:concurrency-control max-sessions="1" error-if-maximum-exceeded="false"  expired-url="/maxSessionError" />
    </security:session-management>

    <security:custom-filter ref="xunxiSecurityInterceptor" before="FILTER_SECURITY_INTERCEPTOR" />
</security:http>
Run Code Online (Sandbox Code Playgroud)

login.jsp的

<form action="<%=request.getContextPath()%>/j_spring_security_check" method="post" class="login-form"  id="login-form" >
        <label>Username</label>
        <input type="text" placeholder="username" name="j_username"/>
        <label>Password</label>
        <input type="password" placeholder="password" name="j_password"/>
        <label>
        <input type="checkbox" name="_spring_security_remember_me" /> Remember me </label>
        <button type="submit" >
            Login
        </button>
    </div>
</form>
Run Code Online (Sandbox Code Playgroud)

spring-security

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