环境:Applciation服务器:Apache 7.0.54 Java:"1.8.0_05"OS:Mac OS X 10.9.3
库:Spring 3.2 REST应用程序
以下是我在部署期间收到的错误:
localhost.2014.06.09.log
Jun 09, 2014 3:37:47 PM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
Jun 09, 2014 3:37:47 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
Jun 09, 2014 3:37:47 PM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
java.lang.IllegalArgumentException
at org.springframework.asm.ClassReader.<init>(Unknown Source)
at org.springframework.asm.ClassReader.<init>(Unknown Source)
at org.springframework.asm.ClassReader.<init>(Unknown Source)
at org.springframework.core.type.classreading.SimpleMetadataReader.<init>(SimpleMetadataReader.java:52)
at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:80)
at org.springframework.core.type.classreading.CachingMetadataReaderFactory.getMetadataReader(CachingMetadataReaderFactory.java:101)
at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:76)
at org.springframework.context.annotation.ConfigurationClassParser.getImports(ConfigurationClassParser.java:298)
at org.springframework.context.annotation.ConfigurationClassParser.getImports(ConfigurationClassParser.java:300)
at …Run Code Online (Sandbox Code Playgroud) 我一直在玩,CompletableFuture并注意到一件奇怪的事情.
String url = "http://google.com";
CompletableFuture<String> contentsCF = readPageCF(url);
CompletableFuture<List<String>> linksCF = contentsCF.thenApply(_4_CompletableFutures::getLinks);
linksCF.thenAccept(list -> {
assertThat(list, not(empty()));
});
linksCF.get();
Run Code Online (Sandbox Code Playgroud)
如果在我的thenAccept调用中断言失败,则不会传播异常.我尝试了一些更丑陋的东西:
linksCF.thenAccept(list -> {
String a = null;
System.out.println(a.toString());
});
Run Code Online (Sandbox Code Playgroud)
没有任何反应,没有例外传播.我尝试使用类似的方法handle和其他与异常相关的方法CompletableFutures,但是失败了 - 没有按预期传播异常.
当我调试它时CompletableFuture,它确实捕获了这样的异常:
final void internalComplete(T v, Throwable ex) {
if (result == null)
UNSAFE.compareAndSwapObject
(this, RESULT, null,
(ex == null) ? (v == null) ? NIL : v :
new AltResult((ex instanceof CompletionException) ? ex :
new CompletionException(ex))); …Run Code Online (Sandbox Code Playgroud) 我可以想到两种方式:
public static IntStream foo(List<Integer> list)
{
return list.stream().mapToInt(Integer::valueOf);
}
public static IntStream bar(List<Integer> list)
{
return list.stream().mapToInt(x -> x);
}
Run Code Online (Sandbox Code Playgroud)
什么是惯用的方式?也许已经有一个库函数完全符合我的要求?
我的确切方案是批量插入数据库,所以我想累积DOM对象然后每1000个,刷新它们.
我通过将代码放入累加器来检测丰满度然后刷新来实现它,但这似乎是错误的 - 刷新控件应该来自调用者.
我可以将流转换为List然后以迭代方式使用subList,但这似乎也很笨拙.
有一个简洁的方法来处理每n个元素然后继续流,而只处理流一次?
假设我想查看流中是否存在对象,如果它不存在,则抛出异常.我可以做的orElseThrow一种方法是使用该方法:
List<String> values = new ArrayList<>();
values.add("one");
//values.add("two"); // exception thrown
values.add("three");
String two = values.stream()
.filter(s -> s.equals("two"))
.findAny()
.orElseThrow(() -> new RuntimeException("not found"));
Run Code Online (Sandbox Code Playgroud)
反过来呢?如果我想在发现任何匹配时抛出异常:
String two = values.stream()
.filter(s -> s.equals("two"))
.findAny()
.ifPresentThrow(() -> new RuntimeException("not found"));
Run Code Online (Sandbox Code Playgroud)
我可以存储Optional,并在isPresent之后进行检查:
Optional<String> two = values.stream()
.filter(s -> s.equals("two"))
.findAny();
if (two.isPresent()) {
throw new RuntimeException("not found");
}
Run Code Online (Sandbox Code Playgroud)
有没有办法实现这种ifPresentThrow行为?试图以这种方式投掷一个不好的做法?
我有一个文件,其中包含以下格式的数据
1
2
3
Run Code Online (Sandbox Code Playgroud)
我想加载它来映射为 {(1->1), (2->1), (3->1)}
这是Java 8代码,
Map<Integer, Integer> map1 = Files.lines(Paths.get(inputFile))
.map(line -> line.trim())
.map(Integer::valueOf)
.collect(Collectors.toMap(x -> x, x -> 1));
Run Code Online (Sandbox Code Playgroud)
我收到以下错误
Exception in thread "main" java.lang.IllegalStateException: Duplicate key 1
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个错误?
我正在尝试将Spring Data JPA 1.8与Java 8 Date/Time API JSR-310一起使用.
一切似乎都有效,直到我试图让所有车辆在两个LocalDateTimes之间.返回的实体数量似乎只与它应该的数量松散相关.
@Repository
public interface VehicleRepository extends JpaRepository<Vehicle, Long> {
List<Vehicle> findByDateTimeBetween(LocalDateTime begin, LocalDateTime end);
}
Run Code Online (Sandbox Code Playgroud)
@Entity
@Table(name = "VEHICLE")
public class Vehicle implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "IDX", nullable = false, unique = true)
@GeneratedValue(strategy = GenerationType.AUTO)
private long vehicleId;
@Column(name = "DATE_TIME", nullable = false)
private LocalDateTime dateTime = LocalDateTime.now();
// Getters and Setters
}
Run Code Online (Sandbox Code Playgroud)
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.8.0.RELEASE</version>
</dependency> …Run Code Online (Sandbox Code Playgroud) 使用以下结构是否存在差异,除了后者的可读性稍好一些?
someList.stream().map(item -> new NewClass(item)).collect(Collectors.toList());
someList.stream().map(NewClass::new).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud) 关于lambdas及其相关的异常签名,我有一个关于Java 8推断的问题.
如果我定义一些方法foo:
public static <T> void foo(Supplier<T> supplier) {
//some logic
...
}
Run Code Online (Sandbox Code Playgroud)
然后,我得到了foo(() -> getTheT());在大多数情况下能够为给定的内容编写的漂亮而简洁的语义T.但是,在此示例中,如果我的getTheT操作声明了它throws Exception,我的foo方法使得供应商不再编译:供应商方法签名get不会抛出异常.
这似乎是解决这个问题的一个不错的方法是重载foo以接受任一选项,重载定义为:
public static <T> void foo(ThrowingSupplier<T> supplier) {
//same logic as other one
...
}
Run Code Online (Sandbox Code Playgroud)
其中ThrowingSupplier定义为
public interface ThrowingSupplier<T> {
public T get() throws Exception;
}
Run Code Online (Sandbox Code Playgroud)
通过这种方式,我们有一个引发异常的供应商类型和一个不引发异常的供应商类型.所需的语法将是这样的:
foo(() -> operationWhichDoesntThrow()); //Doesn't throw, handled by Supplier
foo(() -> operationWhichThrows()); //Does throw, handled by ThrowingSupplier
Run Code Online (Sandbox Code Playgroud)
但是,这会导致问题,因为lambda类型不明确(可能无法在Supplier和ThrowingSupplier之间解决).做一个明确的演员foo((ThrowingSupplier)(() -> operationWhichThrows()));可以工作,但它摆脱了所需语法的大部分简洁性.
我想基本的问题是:如果Java编译器能够解决我的一个lambdas由于它在仅供应商案例中抛出异常而不兼容的事实,为什么它不能使用相同的信息来导出二级,类型推理案例中lambda的类型?
任何人都可以指出的任何信息或资源同样值得赞赏,因为我不太确定在哪里可以找到有关此事的更多信息. …
我正在检查RXJava的文档,我注意到concat和merge运算符似乎也是这样.我写了几个测试以确定.
@Test
public void testContact() {
Observable.concat(Observable.just("Hello"),
Observable.just("reactive"),
Observable.just("world"))
.subscribe(System.out::println);
}
@Test
public void testMerge() {
Observable.merge(Observable.just("Hello"),
Observable.just("reactive"),
Observable.just("world"))
.subscribe(System.out::println);
}
Run Code Online (Sandbox Code Playgroud)
文件说
Merge运算符也类似.它结合了两个或多个Observable的发射,但可以交错它们,而Concat从不交错来自多个Observable的发射.
但是我还是不完全明白,运行这个测试千次,合并结果总是一样的.由于订单未被授予,我期待有时"反应性""世界""你好".
java-8 ×10
java ×9
java-stream ×3
exception ×2
lambda ×2
boxing ×1
chunking ×1
collections ×1
java-time ×1
jpa ×1
rx-java ×1
spring ×1
spring-data ×1
tomcat7 ×1