小编Nim*_*sky的帖子

Guava库:它最有用和/或隐藏的功能是什么?

我有过的快速扫描番石榴API,它提供了新的集合类型(Multimap以及BiMap例如出现有用),我想包括在项目(S)我工作的图书馆.

然而,如果它们没有什么好处并且学习这些功能浪费了宝贵的时间,我也不愿意将这些图书馆包括在内.

您是否已将Guava库包含在项目中并且以任何意外方式证明它有用吗?你将来会一直使用它吗?它的主要优点/节省时间是什么?它隐藏的功能是什么?

java guava

131
推荐指数
8
解决办法
4万
查看次数

使用Ajax将@RequestBody中的多个变量传递给Spring MVC控制器

是否有必要包裹背衬物体?我想做这个:

@RequestMapping(value = "/Test", method = RequestMethod.POST)
@ResponseBody
public boolean getTest(@RequestBody String str1, @RequestBody String str2) {}
Run Code Online (Sandbox Code Playgroud)

并使用这样的JSON:

{
    "str1": "test one",
    "str2": "two test"
}
Run Code Online (Sandbox Code Playgroud)

但相反,我必须使用:

@RequestMapping(value = "/Test", method = RequestMethod.POST)
@ResponseBody
public boolean getTest(@RequestBody Holder holder) {}
Run Code Online (Sandbox Code Playgroud)

然后使用这个JSON:

{
    "holder": {
        "str1": "test one",
        "str2": "two test"
    }
}
Run Code Online (Sandbox Code Playgroud)

那是对的吗?我的另一个选项是改变RequestMethodGET和使用@RequestParam的查询字符串,或者使用@PathVariable与两种RequestMethod.

java spring http spring-mvc

91
推荐指数
5
解决办法
16万
查看次数

如何在多列中找到重复项?

所以我想做下面的sql代码:

select s.id, s.name,s.city 
from stuff s
group by s.name having count(where city and name are identical) > 1
Run Code Online (Sandbox Code Playgroud)

要产生以下内容,(但忽略只有名称或仅匹配城市的地方,它必须在两列上):

id      name  city   
904834  jim   London  
904835  jim   London  
90145   Fred  Paris   
90132   Fred  Paris
90133   Fred  Paris
Run Code Online (Sandbox Code Playgroud)

sql sql-server sql-server-2008

87
推荐指数
6
解决办法
13万
查看次数

使用Java 8的Monads

为了帮助理解monad是什么,有人可以使用java提供一个例子吗?他们有可能吗?

如果你从这里下载预发布的lambda兼容JDK8,可以使用java表达lambda表达式http://jdk8.java.net/lambda/

使用此JDK的lambda示例如下所示,有人可以提供相对简单的monad吗?

public interface TransformService {
        int[] transform(List<Integer> inputs);
    }
    public static void main(String ars[]) {
        TransformService transformService = (inputs) -> {
            int[] ints = new int[inputs.size()];
            int i = 0;
            for (Integer element : inputs) {
                ints[i] = element;
            }
            return ints;
        };

        List<Integer> inputs = new ArrayList<Integer>(5) {{
            add(10);
            add(10);
        }};
        int[] results = transformService.transform(inputs);
    }
Run Code Online (Sandbox Code Playgroud)

java monads optional java-8

75
推荐指数
5
解决办法
3万
查看次数

以编程方式使用Spring调度作业(动态设置fixedRate)

目前我有这个:

@Scheduled(fixedRate=5000)
public void getSchedule(){
   System.out.println("in scheduled job");
}
Run Code Online (Sandbox Code Playgroud)

我可以更改它以使用对属性的引用

@Scheduled(fixedRate=${myRate})
public void getSchedule(){
   System.out.println("in scheduled job");
}
Run Code Online (Sandbox Code Playgroud)

但是,我需要使用以编程方式获得的值,以便可以在不重新部署应用程序的情况下更改计划.什么是最好的方法?我意识到使用注释可能是不可能的......

java spring scheduled-tasks spring-annotations

63
推荐指数
5
解决办法
7万
查看次数

休眠4和joda-time

他们幸福地结婚了吗?

我正在使用最新版本的hibernate(4)和joda-time hibernate支持的 1.3版本,我也相信它是当前的最新版本.

使用注释时,一切似乎都正常工作(按预期创建日期列):

@Column
@Type(type="org.joda.time.contrib.hibernate.PersistentLocalDate")
private LocalDate myDate; 
Run Code Online (Sandbox Code Playgroud)

将这些版本一起使用是否存在任何已知问题?

更新 确定列已创建但无法填充任何数据:

处理程序处理失败; 嵌套异常是java.lang.AbstractMethodError:org.joda.time.contrib.hibernate.PersistentLocalDateTime.nullSafeSet

它们是不兼容的,我应该使用usertype.见下面的答案.

java hibernate jodatime

62
推荐指数
2
解决办法
3万
查看次数

使用spring security以编程方式登录用户

与之相反:如何使用spring security手动注销用户?

在我的应用程序中,我已经注册了新用户屏幕,该屏幕发布到控制器,该控制器在db中创建新用户(并进行一些明显的检查).然后我希望这个新用户自动登录...我有点想要一些东西像这样 :

SecurityContextHolder.getContext().setPrincipal(MyNewUser);
Run Code Online (Sandbox Code Playgroud)

编辑 好我几乎已经基于如何以Spring Security 3.1以编程方式登录用户的答案实现

 Authentication auth = new UsernamePasswordAuthenticationToken(MyNewUser, null);
 SecurityContextHolder.getContext().setPrincipal(MyNewUser);
Run Code Online (Sandbox Code Playgroud)

但是,在部署时,jsp无法访问我,MyNewUser.getWhateverMethods()而在正常登录过程之后也是如此.这个代码在名义上有效,但在登录时会抛出错误如下:

<sec:authentication property="principal.firstname" /> 
Run Code Online (Sandbox Code Playgroud)

spring-mvc spring-security

59
推荐指数
3
解决办法
4万
查看次数

Spring 3.1,Hibernate 4,SessionFactory

这是有效的:

<bean id="sessionFactory"  
      class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
...
Run Code Online (Sandbox Code Playgroud)

但升级到上述版本打破了它.使用Spring 3.1.ReleaseHibernate 4.0.0.FINAL创建SessionFactory bean 的正确方法是什么?

部署错误是:

嵌套异常是java.lang.NoClassDefFoundError:Lorg/hibernate/cache/CacheProvider;


编辑
添加了我自己的答案,为我修复了它.

java spring hibernate

42
推荐指数
4
解决办法
8万
查看次数

没有为响应类型找到合适的HttpMessageConverter

使用spring,使用以下代码:

List<HttpMessageConverter<?>> messageConverters = restTemplate.getMessageConverters();
for(HttpMessageConverter httpMessageConverter : messageConverters){
  System.out.println(httpMessageConverter);
}
ResponseEntity<ProductList> productList = restTemplate.getForEntity(productDataUrl,ProductList.class);
Run Code Online (Sandbox Code Playgroud)

我明白了

org.springframework.http.converter.ByteArrayHttpMessageConverter@34649ee4
org.springframework.http.converter.StringHttpMessageConverter@39fba59b
org.springframework.http.converter.ResourceHttpMessageConverter@383580da
org.springframework.http.converter.xml.SourceHttpMessageConverter@409e850a
org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter@673074aa
org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter@1e3b79d3
org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@52bb1b26

org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class com.mycopmany.ProductList] and content type [text/html;charset=UTF-8]
Run Code Online (Sandbox Code Playgroud)

pojo的片段:

@XmlRootElement(name="TheProductList")
public class ProductList {

@XmlElement(required = true, name = "date")
private LocalDate importDate;
Run Code Online (Sandbox Code Playgroud)

java xml spring jaxb

39
推荐指数
5
解决办法
12万
查看次数

@ManyToMany(mappedBy ="foo")

Foo有:

@ManyToMany(mappedBy = "foos")
private Set<Bar> bars
Run Code Online (Sandbox Code Playgroud)

和酒吧有:

@ManyToMany
private Set<Foo> foos
Run Code Online (Sandbox Code Playgroud)

除了table被称为foo_bar或bar_foo之外,mappedBy属性的位置与双向关系有什么不同; 如果没有mappedBy属性,我会得到两个连接表,包括foo_bar和bar_foo.

java orm many-to-many jpa

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