从Java 1.7开始,字符串可以与switch语句一起使用,让我对某些事情感到疑惑.switch关于整数值的语句可以转换为跳转表,这比仅仅if对运行时计算的整数进行检查更快; 可以用字符串进行类似的优化,还是这只是语法糖?
我试图在我的注释上面添加换行符在XML文档中的根节点之上.
我需要这样的东西:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!--DO NOT EDIT THIS FILE-->
<projects>
</projects>
Run Code Online (Sandbox Code Playgroud)
但是我能得到的是这个(在根中断行但我需要在评论后换行):
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!--DO NOT EDIT THIS FILE--><projects>
</projects>
Run Code Online (Sandbox Code Playgroud)
我需要在评论之后添加换行符.有没有办法做到这一点?
我的代码:
import java.io.File;
import java.io.FileInputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Comment;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
public class XMLNewLine {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("Adding comment..");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(false);
DocumentBuilder db;
try {
Document doc; …Run Code Online (Sandbox Code Playgroud) 我正在学习反思.当我执行以下代码时:
package main;
import java.lang.reflect.Field;
public class Main {
public static void main(String[] args) throws NoSuchFieldException, SecurityException {
Base firstBase = new Base();
Field firstBaseField = firstBase.getClass().getDeclaredField("protectedBuffer");
System.out.println(firstBaseField.isAccessible());
}
}
Run Code Online (Sandbox Code Playgroud)
这是基类:
package main;
public class Base {
private StringBuffer buffer;
protected StringBuffer protectedBuffer;
public StringBuffer buffer2;
}
Run Code Online (Sandbox Code Playgroud)
结果是错误的.但这不是真的,因为我可以这样访问protectedBuffer:firstBase.protectedBuffer?
我有一个 spring MVC 端点返回StreamingResponseBody,因此我可以流式传输大文件而不是将其保留在内存中。
该请求可能需要一段时间才能下载,因此超时。我可以通过设置来修复它spring.mvc.async.request-timeout=360000,但我不希望此设置全局应用于所有端点。
有没有办法仅为该特定端点设置请求超时?
我确实在 Spring-boot 中找到了Set timeout for certain async request,但我认为这不适用于我使用StreamingResponseBody.
我还发现Spring REST 端点在 30 秒后返回 StreamingResponseBody: AsyncRequestTimeoutException,这没有达到预期的效果。
这是我的源代码:
public class Koray {
public static void main(String [] args) {
System.out.println("This is a sample program.");
}
}
Run Code Online (Sandbox Code Playgroud)
当我编译它时,我得到字节码.当我用十六进制查看器查看字节码时,我看到部分:
19 54 68 69 73 20 69 73 20 61 20 73 61 6D 70 6C 65 20 70 72 6F 67 72 61 6D 2E
Run Code Online (Sandbox Code Playgroud)
可以读作
This is a sample program.
Run Code Online (Sandbox Code Playgroud)
如果字节被解释为字符.
而当我这样做
javap -c Koray.class
Run Code Online (Sandbox Code Playgroud)
我拆开这个课我看到:
Compiled from "Koray.java"
public class Koray {
public Koray();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public static …Run Code Online (Sandbox Code Playgroud) 我有一个单元测试,需要检查嵌套的映射值.我可以通过拉出条目并匹配底层来让我的断言工作Map,但我正在寻找一种明确的方式来显示断言正在做什么.这是一个非常简化的测试:
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasEntry;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
public class MapContainsMapTest {
@Test
public void testMapHasMap() {
Map<String, Object> outerMap = new HashMap<String, Object>();
Map<String, Object> nestedMap = new HashMap<String, Object>();
nestedMap.put("foo", "bar");
outerMap.put("nested", nestedMap);
// works but murky
assertThat((Map<String, Object>) outerMap.get("nested"), hasEntry("foo", "bar"));
// fails but clear
assertThat(outerMap, hasEntry("nested", hasEntry("foo", "bar")));
}
}
Run Code Online (Sandbox Code Playgroud)
似乎问题是外部地图正在使用,hasEntry(K key, V value)而我想要使用的是hasEntry(Matcher<? super K> keyMatcher, Matcher<? super V> valueMatcher).我不知道如何强制断言使用第二种形式.
提前致谢.
我有以下ant脚本,当对于antcontrib的foreach任务将parallel设置为true时,我似乎找不到失败的方法.有任何想法吗?
<project name="asdf" >
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
<pathelement location="../lib/ant/ant-contrib-1.0b3.jar" />
</classpath>
</taskdef>
<target name="build">
<foreach target="exex-subant" param="foreach.dir" parallel="true" maxthreads="4" inheritall="true" list="1,2,3">
<param name="target" value="build" />
</foreach>
</target>
<target name="exex-subant">
<fail>test</fail>
</target>
</project>
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用javax.activation.MimetypesFileTypeMap以获取内容类型.
对于字符串"image.png",它总是返回"application/octect-stream"...它不应该返回像"image/png"这样的东西吗?
javax.activation.MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType("image.png");
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Maven创建一个Web应用程序项目,如下所述,但我一直收到以下错误
mvn archetype:create
-DgroupId=ru.jofsey
-DartifactId=example
-DarchetypeArtifactId=maven-archetype-webapp
-DinteractiveMode=false
-e
[INFO] Error stacktraces are turned on.
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.373 s
[INFO] Finished at: 2017-02-18T14:31:49+03:00
[INFO] Final Memory: 7M/123M
[INFO] ------------------------------------------------------------------------
[ERROR] Could not find goal 'create' in plugin org.apache.maven.plugins:maven-archetype-plugin:3.0.0 among available goals crawl, create-from-project, generate, help, integration-test, jar, update-local-catalog -> [Help 1]
org.apache.maven.plugin.MojoNotFoundException: Could not find goal 'create' in plugin org.apache.maven.plugins:maven-archetype-plugin:3.0.0 among available goals crawl, create-from-project, generate, help, …Run Code Online (Sandbox Code Playgroud) 我无法弄清楚为什么我从我的ant build.xml文件中获取此异常.我检查过,一切都在类路径中.为什么要这么复杂?!
我过去遇到过Ant问题,似乎它总是与类路径有关.我使用两种方式指向junit.jar:在eclipse:window-> preferences-> ant-> runtime-> Ant Home-> Add External Jars,以及build.xml脚本中.这次Ant无法在junit任务中找到我的测试类.我指向这门课的方式有问题吗?
<target name="init">
<property name="sourceDir" value="src"/>
<property name="outputDir" value="build" />
<property name="junitLocation" value="C:\...\org.junit4_4.3.1\junit.jar" />
</target>
<target name="clean" depends="init">
<delete dir="${outputDir}" />
</target>
<target name="prepare" depends="clean">
<mkdir dir="${outputDir}" />
</target>
<target name="compile" depends="prepare">
<javac srcdir="${sourceDir}" destdir="${outputDir}" classpath="${junitLocation}"/>
</target>
<path id="classpath">
<pathelement location="${outputDir}" />
<pathelement location="${junitLocation}" />
</path>
<target name="testApplication" depends="compile">
<echo>Running the junit tests...</echo>
<junit fork="yes" haltonfailure="yes">
<test name="my.package.MyTest" />
<formatter type="plain" usefile="false" />
<classpath refid="classpath" />
</junit>
</target>
Run Code Online (Sandbox Code Playgroud)
我总是得到:
[junit] …Run Code Online (Sandbox Code Playgroud) java ×8
ant ×2
junit ×2
string ×2
ant-contrib ×1
asynchronous ×1
bytecode ×1
domparser ×1
foreach ×1
hamcrest ×1
jvm ×1
line-breaks ×1
maven ×1
mime ×1
reflection ×1
spring ×1
spring-mvc ×1
timeout ×1
xml ×1