小编Did*_*r L的帖子

是否可以在Spring Repository中使用原始SQL

我需要在Spring Data Repository中使用原始SQL,这可能吗?我看到的一切@Query都是基于实体的.

java hibernate spring-data-jpa

95
推荐指数
5
解决办法
15万
查看次数

CompletableFuture | 然后应用vs thenCompose

我无法理解thenApply(和之间的区别thenCompose().

那么,有人可以提供有效的用例吗?

来自Java文档:

thenApply(Function<? super T,? extends U> fn)
Run Code Online (Sandbox Code Playgroud)

返回一个新的CompletionStage,当该阶段正常完成时,将使用此阶段的结果作为所提供函数的参数执行.

thenCompose(Function<? super T,? extends CompletionStage<U>> fn)
Run Code Online (Sandbox Code Playgroud)

返回一个新的CompletionStage,当此阶段正常完成时,将使用此阶段作为所提供函数的参数执行.

我得到的第二个参数thenCompose扩展了CompletionStage,thenApply而不是.

有人可以提供一个例子,我必须使用thenApply何时使用thenCompose

java java-8 completable-future

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

将输入流连接到输出流

java9中的更新:https://docs.oracle.com/javase/9​​/docs/api/java/io/InputStream.html#transferTo-java.io.OutputStream-

我看到了一些相似的,但不是我需要的线程.

我有一个服务器,它基本上从客户端,客户端A接收输入,并逐字节转发到另一个客户端,客户端B.

我想将客户端A的输入流与客户端B的输出流连接起来.这可能吗?有什么方法可以做到这一点?

此外,这些客户端正在发送彼此有时间敏感的消息,因此缓冲不会.我不想要500的缓冲区和客户端发送499个字节,然后我的服务器在转发500个字节时保持关闭,因为它没有收到填充缓冲区的最后一个字节.

现在,我正在解析每条消息以找到它的长度,然后读取长度字节,然后转发它们.我想(并测试)这比读取一个字节并反复转发一个字节更好,因为这将非常慢.我也不想使用缓冲区或计时器,因为我在上一段中说过 - 我不希望消息等待很长时间才能通过,因为缓冲区未满.

这样做的好方法是什么?

java inputstream outputstream

72
推荐指数
6
解决办法
11万
查看次数

仅在Firefox上"带有源的<script>加载失败"

我想将Marketo表单与我在yii框架上的现有网站集成.我的代码适用于除Firefox之外的所有浏览器.

摘自我的代码:

    $('#button').click(function () {
    var formData = {
        'Email': $('#UserInfo_email').val(),
        'FirstName': $('#UserInfo_first_name').val(),
        'LastName': $('#UserInfo_last_name').val(),
        };
    MktoForms2.loadForm('//app-ab23.marketo.com', mcId, formId, function (form) {
        var myForm = MktoForms2.allForms()[0];
        myForm.addHiddenFields(formData);
        myForm.onSuccess(function (values, followUpUrl) {
            return false;
        });
        myForm.submit();
    });
});
Run Code Online (Sandbox Code Playgroud)

我只在消息上遇到Firefox错误

带有源" http://app-ab23.marketo.com/index.php/form/getForm?munchkinId=1111&form=1111&url=http%3A%2F%2Fblox.dev%2Fwizard%2Fmap&callback=的<script>加载失败jQuery110207175825035737486_1503656391790&_ = 1503656391791 ".

其他浏览器正确完成工作

注意:munchkinId和formId已更改,可在此处发布.

javascript php firefox jquery yii

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

通用类型推断无法使用方法链接?

此代码在Java 8中编译,但无法在Java 7中编译:

class Map<K,V> {
    static <K,V> Map<K,V> empty() {return null;}
    Map<K,V> put(K k, V v) {return null;}
    V get(K k) {return null;}
}

class A {
    static void f(Map<Integer,String> m){}
    public static void main(String[] args) {
        f(Map.empty());
    }
}
Run Code Online (Sandbox Code Playgroud)

它不会推断出Map返回的具体类型Map.empty():

$ javac7 A.java
A.java:10: error: method f in class A cannot be applied to given types;
        f(Map.empty());
        ^
  required: Map<Integer,String>
  found: Map<Object,Object>
  reason: actual argument Map<Object,Object> cannot be converted to Map<Integer,String> by method invocation …
Run Code Online (Sandbox Code Playgroud)

java generics type-inference java-7 java-8

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

从CompletableFuture抛出异常

我有以下代码:

// How to throw the ServerException?
public void myFunc() throws ServerException{
    // Some code
    CompletableFuture<A> a = CompletableFuture.supplyAsync(() -> {
        try {
            return someObj.someFunc();
        } catch(ServerException ex) {
            // throw ex; gives an error here.
        }
    }));
    // Some code
}
Run Code Online (Sandbox Code Playgroud)

someFunc()抛出一个ServerException.我不想在这里处理这个问题,而是将异常抛给someFunc()调用者myFunc().

java exception-handling exception java-8 completable-future

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

为什么这个java Stream会运行两次?

Java的API 8说:

在执行管道的终端操作之前,不会开始遍历管道源.

那么为什么以下代码抛出:

java.lang.IllegalStateException:stream已经被操作或关闭

Stream<Integer> stream = Stream.of(1,2,3);
stream.filter( x-> x>1 );
stream.filter( x-> x>2 ).forEach(System.out::print);
Run Code Online (Sandbox Code Playgroud)

根据API的第一个过滤操作不应该在Stream上操作.

java java-8 java-stream

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

尝试使用String.split("\\?")时出现意外行为

所以我有一个像这样的字符串:

"Some text here?Some number here"
Run Code Online (Sandbox Code Playgroud)

我需要拆分那些,我正在使用String.split("\\?"),但如果我有这样的字符串:

"This is a string with, comma?1234567"
Run Code Online (Sandbox Code Playgroud)

我也把它分成了逗号(,).如果我有这个字符串:

"That´s a problem here?123456"
Run Code Online (Sandbox Code Playgroud)

它也分裂了´,那我怎么解决这个问题呢?

java string split

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

忽略名称空间的JAXB解组将元素属性转换为null

我正在尝试使用JAXB将xml文件解组为对象,但遇到了一些困难.实际项目在xml文件中有几千行,所以我在较小的范围内重现了错误,如下所示:

XML文件:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<catalogue title="some catalogue title" 
           publisher="some publishing house" 
           xmlns="x-schema:TamsDataSchema.xml"/>
Run Code Online (Sandbox Code Playgroud)

用于生成JAXB类的XSD文件

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <xsd:element name="catalogue" type="catalogueType"/>

 <xsd:complexType name="catalogueType">
  <xsd:sequence>
   <xsd:element ref="journal"  minOccurs="0" maxOccurs="unbounded"/>
  </xsd:sequence>
  <xsd:attribute name="title" type="xsd:string"/>
  <xsd:attribute name="publisher" type="xsd:string"/>
 </xsd:complexType>
</xsd:schema>
Run Code Online (Sandbox Code Playgroud)

代码段1:

final JAXBContext context = JAXBContext.newInstance(CatalogueType.class);
um = context.createUnmarshaller();
CatalogueType ct = (CatalogueType)um.unmarshal(new File("file output address"));
Run Code Online (Sandbox Code Playgroud)

哪个引发错误:

javax.xml.bind.UnmarshalException: unexpected element (uri:"x-schema:TamsDataSchema.xml", local:"catalogue"). Expected elements are <{}catalogue>
 at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:642)
 at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:247)
 at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:242)
 at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:116)
 at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1049)
 at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:478)
 at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:459)
 at com.sun.xml.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:148)
 at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(Unknown …
Run Code Online (Sandbox Code Playgroud)

java attributes namespaces jaxb jdk1.5

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

是否有一种方便的方法来创建一个测试字段是否等于给定值的谓词?

我经常发现自己需要过滤Stream或使用谓词来检查给定字段是否具有给定值.

比方说我有这个POJO:

public class A {
    private Integer field;

    public A(final Integer field) {
        this.field = field;
    }

    public Integer getField() {
        return field;
    }
}
Run Code Online (Sandbox Code Playgroud)

我想Stream根据以下值来过滤一个对象field:

    final Integer someValue = 42;
    Stream.of(new A(42), new A(1729), new A(42), new A(87539319), new A(null))
            .filter(a -> Objects.equals(a.getField(), someValue))
            ...
Run Code Online (Sandbox Code Playgroud)

是否有方便的方法来生成方法的谓词filter?我注意到Predicate.isEqual它有但不适合需要.

我可以很容易地写一个这样的:

public static <T,R> Predicate<T> isEqual(Function<? super T, ? extends R> f, R value) {
    return v -> Objects.equals(value, f.apply(v));
}
Run Code Online (Sandbox Code Playgroud)

并将其用作: …

java functional-programming predicate java-8

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