小编the*_*dam的帖子

在Spring Boot中转义Yaml中的Map键中的一个点

我有一个以下的yml配置:

foo:
  bar.com:
    a: b
  baz.com:
    a: c
Run Code Online (Sandbox Code Playgroud)

使用以下类,Spring尝试使用键"bar"和"baz"注入地图,将点视为分隔符:

public class JavaBean {
    private Map<String, AnotherBean> foo;
(...)
}
Run Code Online (Sandbox Code Playgroud)

我试过引用密钥(即'bar.com'或"bar.com"),但无济于事 - 仍然是同样的问题.有没有解决的办法?

spring yaml key spring-boot

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

Apache Commons XMLConfiguration - 如何获取给定节点上的对象列表?

我有一个类似于这样的XML配置文件:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<config>
    <mainServerHostname>MainServer</mainServerHostname>
    <failoverServers>
        <server>
            <ipAddress>192.168.0.5</ipAddress>
            <priority>1</priority>
        </server>
        <server>
            <ipAddress>192.168.0.6</ipAddress>
            <priority>2</priority>
        </server>
    </failoverServers>
</config>
Run Code Online (Sandbox Code Playgroud)

现在,我知道通过使用以下代码(在设置我的XMLConfiguration对象并调用它之后):

config.getList("failoverServers.server.ipAddress");
Run Code Online (Sandbox Code Playgroud)

我可以得到所有IP地址的列表.这很方便,但如果我可以做这样的事情会更方便:

config.getList("failoverServers.server");
Run Code Online (Sandbox Code Playgroud)

并获取一个对象列表,每个对象都有一个ipAddress和一个优先级.据我所知,没有办法做到这一点.有没有人对如何实现这种功能有任何想法?我甚至非常愿意定义与Java可以将数据映射到的XML结构相对应的数据结构,如果这会使事情变得更容易(事实上甚至可能更好).谢谢大家的帮助!

java orm apache-commons

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

Jquery href click - 如何启动事件?

我有这个锚点,点击后我想弹出一些东西.此href位于具有其他href的页面内.

<a class="sign_new" href="#sign_up">Sign up</a>
Run Code Online (Sandbox Code Playgroud)

jQuery的:

$(document).ready(function(){
   $('a[href = "sign_up"]').click(function(){
      alert('Sign new href executed.'); 
   }); 
});
Run Code Online (Sandbox Code Playgroud)

上面的代码没有启动.

html javascript jquery

15
推荐指数
2
解决办法
12万
查看次数

NamedEntityGraph - JPA/Hibernate抛出org.hibernate.loader.MultipleBagFetchException:无法同时获取多个包

我们有一个项目,我们需要懒惰地加载一个实体的集合,但在某些情况下我们需要它们急切地加载它们.我们已经@NamedEntityGraph为我们的实体添加了注释.在我们的存储库方法中,我们添加了一个"javax.persistence.loadgraph"提示,以急切地加载在所述注释中定义的4个属性.当我们调用该查询时,Hibernate抛出org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags.

有趣的是,当我将所有这些集合重新定义为急切获取时,Hibernate 在没有MultipleBagFetchException的情况下急切地获取它们.

这是蒸馏代码.实体:

@Entity
@NamedEntityGraph(name = "Post.Full", attributeNodes = {
        @NamedAttributeNode("comments"),
        @NamedAttributeNode("plusoners"),
        @NamedAttributeNode("sharedWith")
    }
)
public class Post {
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "postId")
    private List<Comment> comments;

    @ElementCollection
    @CollectionTable(name="post_plusoners")
    private List<PostRelatedPerson> plusoners;

    @ElementCollection
    @CollectionTable(name="post_shared_with")
    private List<PostRelatedPerson> sharedWith;

}
Run Code Online (Sandbox Code Playgroud)

查询方法(所有人都挤在一起让它可以发布):

@Override
public Page<Post> findFullPosts(Specification<Post> spec, Pageable pageable) {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Post> query = builder.createQuery(Post.class);
    Root<Post> post = query.from(Post.class);
    Predicate postsPredicate = spec.toPredicate(post, query, builder);
    query.where(postsPredicate); …
Run Code Online (Sandbox Code Playgroud)

java hibernate jpa

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

Java线程池 - 线程正在更新?

我的Java.NIO套接字服务器中有一个线程池.我有时会收到像Connection reset by peerBroken Pipe等的运行时错误.

我的问题是:抛出异常时线程被杀死了吗?如果是 - 是在线程池中创建的新线程代替被杀死的线程?

这是我的ThreadManager:

import java.nio.channels.SocketChannel;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


public class ThreadsManager {

    private ExecutorService threadPool = null;
    private LiveConnectionsManager liveConnectionsManager;
    private int threadPoolSize = 35; //number of tasks thread

    public ThreadsManager(LiveConnectionsManager liveConnectionsManager) {
        this.liveConnectionsManager = liveConnectionsManager;
        threadPool = Executors.newFixedThreadPool(threadPoolSize);
        ServerActions.threadPool = threadPool;
    }

    public void processNewMessage(SocketChannel socketChannel, Client client)
    {
        threadPool.execute(new MessagesProcessor(socketChannel, client, liveConnectionsManager));
    }

    public void closeConnection(SocketChannel socketChannel, Client client) {
        threadPool.execute(new LogoutClient(socketChannel, client, null));
    }   
}
Run Code Online (Sandbox Code Playgroud)

java multithreading

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

Angular JS生成PDF - 任何创建者 - 制造商模块?

正如标题所说,Angular有任何PDF创建者/生成器吗?

我见过https://github.com/MrRio/jsPDF,但找不到任何Angular.我想制作一个HTML页面到pdf文件下载.

javascript pdf angularjs meanjs

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

document.ready - jQuery时没有警告消息

这是我的代码:

$(document).ready(function() {
    alert("I am an alert box!");
});
Run Code Online (Sandbox Code Playgroud)

当我刷新页面时,没有警告框.有什么不对吗?

jQuery似乎被正确添加,但我无法获得检查jQuery是否正常工作的警报.有关如何制作警报的任何想法?

jquery

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

Mockito - 模拟混凝土类

给出以下代码:

    LinkedList list = mock(LinkedList.class);
    doCallRealMethod().when(list).clear();
    list.clear();
Run Code Online (Sandbox Code Playgroud)

通过执行此测试,从LinkedList#clear的第一行抛出NullPointerException:

public void clear() {
    Entry<E> e = header.next;
    while (e != header) {
        Entry<E> next = e.next;
        //Code omitted. 
Run Code Online (Sandbox Code Playgroud)

但是标题之前已被实例化:

private transient Entry<E> header = new Entry<E>(null, null, null);
Run Code Online (Sandbox Code Playgroud)

有人可以解释模拟创作过程中发生了什么吗?

#######更新.######

在阅读了所有答案,特别是Ajay的答案之后,我查看了Objenesis源代码并发现它使用Reflection API来创建代理实例(通过CGLIB),因此绕过层次结构中的所有构造函数,直到java.lang.Object.

以下是模拟问题的示例代码:

public class ReflectionConstructorTest {

    @Test
    public void testAgain() {

        try {
            //java.lang.Object default constructor
            Constructor javaLangObjectConstructor = Object.class
                    .getConstructor((Class[]) null);
            Constructor mungedConstructor = ReflectionFactory
                    .getReflectionFactory()
                    .newConstructorForSerialization(CustomClient.class, javaLangObjectConstructor);

            mungedConstructor.setAccessible(true);

            //Creates new client instance without calling its constructor …
Run Code Online (Sandbox Code Playgroud)

java unit-testing mocking mockito

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

当参数以(.pl)结尾时,为什么Spring MVC @RequestMapping会因映射(/user/{username:.+})而抛出406错误

@RequestMapping(value = "/user/{username:.+}", method = RequestMethod.GET, produces = "application/json")
@ResponseBody
User user(@PathVariable String username) {
    User user = userRepository.findByUsername(username);
    if (user == null)
        throw new UserNotFoundException("User not found");

    return user;
}
Run Code Online (Sandbox Code Playgroud)

这是表示该动作的方法.控制器注释为@RestController

解决了

应覆盖内容类型协商机制.

Explonation:http://spring.io/blog/2013/05/11/content-negotiation-using-spring-mvc

码:

@Override
  public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    configurer.mediaType("pl", MediaType.APPLICATION_JSON);
  }
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc spring-boot

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

如何使Spring Data Elasticsearch与java.time.LocalDateTime一起使用以获取日期

我正在使用Spring Data对Elasticsearch的支持。这是时间戳字段映射:

@Field(type = FieldType.Date, index = FieldIndex.not_analyzed, store = true,
        format = DateFormat.custom, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZZ")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern ="yyyy-MM-dd'T'HH:mm:ss.SSSZZ")
private LocalDateTime timestamp;
Run Code Online (Sandbox Code Playgroud)

这导致Elasticsearch中的字段映射如下:

"timestamp":{"type":"date","store":true,"format":"yyyy-MM-dd'T'HH:mm:ss.SSSZZ"}
Run Code Online (Sandbox Code Playgroud)

当我使用java.util.Date时,一切正常。但是,当我如上所述切换到java.time.LocalDateTime时,发送到Elasticsearch的文档导致异常。这是文档(为简洁起见,时间戳字段):

"timestamp": {
    "hour":7, "minute":56, "second":9, "nano":147000000, "year":2017, "month":"FEBRUARY",
    "dayOfMonth":13, "dayOfWeek":"MONDAY", "dayOfYear":44, "monthValue":2, "chronology": {
        "id":"ISO", "calendarType": "iso8601"
    }
}
Run Code Online (Sandbox Code Playgroud)

和例外:

MapperParsingException[failed to parse [timestamp]]; nested: IllegalArgumentException[unknown property [hour]];
(...)
Caused by: java.lang.IllegalArgumentException: unknown property [hour]
Run Code Online (Sandbox Code Playgroud)

似乎在对文档进行json加密时,这里会忽略该模式。有任何提示吗?也许您可能知道如何在Spring Data中使用“内置” _timestamp字段?

java spring elasticsearch spring-data

5
推荐指数
2
解决办法
5084
查看次数

Java中的本地化日期格式

我有一个毫秒的时间戳,并希望格式化它表示日,月,年和小时的分钟精确度.

我知道我可以指定这样的格式:

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yy HH:mm");
String formatted = simpleDateFormat.format(900000)
Run Code Online (Sandbox Code Playgroud)

但我希望将格式与用户的语言环境进行本地化.我也试过了

DateFormat DATE_FORMAT = DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault());
DATE_FORMAT.format(new Date());
Run Code Online (Sandbox Code Playgroud)

但它没有显示时间.我该怎么做?

java datetime-format

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