我正在尝试扩展我的库以进行集成Swing,JPA并将JPA配置设置为可自动(和可移植),这意味着以编程方式添加<class>元素.(我知道它可以通过Hibernate AnnotationConfiguration或EclipseLInk 完成ServerSession,但是 - 可移植性).我也想避免Spring仅仅为了这个目的而使用.
我可以动态创建persistence.xml,并使用<class>指定包中的元素(通过Reflections库)填充它.当我尝试将此persistence.xml提供给JPA提供程序时,问题就开始了.我能想到的唯一方法是设置一个URLClassLoader,但是我想不出一种方法,首先不会让我把文件写到磁盘上,因为它有能力获得有效的URL.设置套接字通过URL(localhost:xxxx)服务文件似乎......我不知道,邪恶?
有谁知道如何解决这个问题?我知道避免使用一个库听起来很多工作,但我只想知道它是否可以完成.
编辑(尝试更清楚):
动态生成XML保存在String对象中.我不知道如何使它可用于持久性提供程序.另外,我想避免将文件写入磁盘.
出于我的问题的目的,持久性提供程序只是一个扫描类路径的类META-INF/persistence.xml.可以使一些实现接受动态创建XML,但是没有通用接口(特别是对于文件的关键部分,<class>标签).
我的想法是建立一个习俗ClassLoader- 如果你有任何其他的我会感激,我不会在这个上面.
我能找到的唯一容易扩展/可配置的是a URLClassLoader.它适用于URL对象,我不知道是否可以创建一个没有实际首先将XML写入磁盘.
这就是我正在设置的方式,但它是通过写入persistenceXmlFile = new File("META-INF/persistence.xml")磁盘来实现的:
Thread.currentThread().setContextClassLoader(
new URLResourceClassLoader(
new URL[] { persistenceXmlFile.toURI().toURL() },
Thread.currentThread().getContextClassLoader()
) …Run Code Online (Sandbox Code Playgroud) 可以抛出也可以用来退出switch语句而不使用break关键字吗?为什么要使用投掷而不是休息?
switch(number)
{
case 1:
throw new RuntimeException("Exception number 1");
case 2:
throw new RuntimeException("Exception number 2");
}
Run Code Online (Sandbox Code Playgroud) 目前我正在努力学习Angular JS,但首先我想设置我的环境Spring mvc.
目前我只想休息,但我怀疑将资源放入的最佳方式是什么 Spring MVC
我简单的应用程序有这个口号:
my-simple-app:
src
main
java
resources
webapp
resources
WEB-INF
Run Code Online (Sandbox Code Playgroud)
如果我想把角度种子中的app文件夹放进去,最好把它放在哪里?
我试图放入,src/webapp/resources/app但后来我必须将html文件移动到WEB-INF?
你的angular-js spring mvc应用程序中的骨架怎么样?
重定向到的最佳方法是什么app/index.html?到欢迎文件,然后只与angularjs $routeproviders?
谢谢!
我正在使用Spring MVC并返回JSON作为响应。我想创建一个通用JSON响应,我可以在其中输入任何类型并希望响应看起来像这样
{
status : "success",
data : {
"accounts" : [
{ "id" : 1, "title" : "saving", "sortcode" : "121212" },
{ "id" : 2, "title" : "current", "sortcode" : "445566" },
]
}
}
Run Code Online (Sandbox Code Playgroud)
所以我创建了一个Response<T>对象
public class Response<T> {
private String status;
private String message;
T data;
...
...
}
Run Code Online (Sandbox Code Playgroud)
Spring控制器中使用此 Response 对象来返回空响应对象和/或填充的响应对象。预先感谢 GM
更新:
为了获得JSON与所描述的类似的输出,即使用“accounts”键JSON,我必须Response<Map<String, List<Account>>>在控制器中使用以下内容:
@RequestMapping(value = …Run Code Online (Sandbox Code Playgroud) 我正试图用球衣做一个宁静的服务,因为我正在使用球衣示例进行一个maven项目.所以这就是我得到的:
我的pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>simple-service-webapp</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>simple-service-webapp</name>
<build>
<finalName>simple-service-webapp</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
<!-- uncomment this to get JSON support <dependency> <groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId> </dependency> -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId> …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用一个XmlAdapterunmarshal XML到一个自定义集合类型(Collection由于另一个类的使用所施加的冲突限制,故意不实现接口,如果该类包含任何集合,这很可能会失败).难点在于XML放置在集合中的节点不会包装在包装元素中.
这是我的XML基本面貌:
<xml>
<foo></foo>
<bar>1</bar>
<bar>2</bar>
</xml>
Run Code Online (Sandbox Code Playgroud)
我可以JAXB解决以下问题POJO:
class Naive {
Foo foo;
List<Bar> bar;
}
Run Code Online (Sandbox Code Playgroud)
但是,我想要做的是以下内容:
class Bars {
// There will be a fixed number of these, known in advance
Bar bar1;
Bar bar2;
}
class BarsAdapter extends XmlAdapter<ArrayList<Bar>, Bars> {
...
}
class Custom {
Foo foo;
@XmlJavaTypeAdapter(BarsAdapter.class) // won't work
Bars bar;
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我编写了一个XmlAdapter想要调整整个列表的内容,而不是单个元素,但它从不会被调用以进行解组.(它会被调用以进行编组.)
如果我XML在<bar>元素周围包含一个包装器,那么我知道如何解决这个问题:
<xml> …Run Code Online (Sandbox Code Playgroud) 我在Java简单项目中有一个应用程序.但是,我需要将此项目粘贴到Maven项目中.所以,我基本上制作了一个简单的Maven项目,并将所有类复制并粘贴到其中.我需要在服务器中运行一个war,我需要像Java应用程序一样运行Main,因为这个应用程序配置了war应用程序.但是,当我运行Main时,我会收到一些我之前没有的错误:
java.io.FileNotFoundException:resources\config.properties(系统找不到指定的路径)
在代码中是:
input = new FileInputStream("resources/config.properties");
Run Code Online (Sandbox Code Playgroud)
这也不起作用:
faceDetector = new CascadeClassifierDetector("D:/retinoblastoma/workspace/Resources/CascadeClassifiers/FaceDetection/haarcascade_frontalface_alt.xml");
Run Code Online (Sandbox Code Playgroud)
我怎样才能解决这个问题?
通过这里回答的几个例子和问题(主要是 http://www.javaworld.com/javaworld/jw-02-2009/jw-02-servlet3.html?page=3),我想让服务器发送多个响应没有完成请求的客户的时间.当请求超时时,我创建另一个,依此类推.
我想避免长时间轮询,因为每次收到响应时我都必须重新创建请求.(而且这不是servlet 3.0的异步功能所针对的).
我在服务器端有这个:
@WebServlet(urlPatterns = {"/home"}, name = "async", asyncSupported = true)
public class CometServlet extends HttpServlet {
public void doGet(final HttpServletRequest request, final HttpServletResponse response) throws IOException, ServletException {
AsyncContext ac = request.startAsync(request, response);
HashMap<String, AsyncContext> store = AppContext.getInstance().getStore();
store.put(request.getParameter("id"), ac);
}
}
Run Code Online (Sandbox Code Playgroud)
还有一个写入异步上下文的线程.
class MyThread extends Thread {
String id, message;
public MyThread(String id, String message) {
this.id = id;
this.message = message;
}
public void run() {
HashMap<String, AsyncContext> store = AppContext.getInstance().getStore();
AsyncContext ac …Run Code Online (Sandbox Code Playgroud) 我有一个查询方法,其中参数作为文件名出现,在我分析调试时,如下所示:
private processfile ( string filePath)
{
}
Run Code Online (Sandbox Code Playgroud)
现在这个文件路径可以是:
C:\abc\file1.txt
or
C:\abc\def\file1.txt
or
C:\ghj\ytr\wer\file1.txt
Run Code Online (Sandbox Code Playgroud)
现在我的查询是我必须只提取文件名并且必须存储在字符串参数中.所以我必须存储file1.txt一个字符串,比如说在一个字符串参数中s,所以最后s将存储为
String s = file1.txt
Run Code Online (Sandbox Code Playgroud)
怎么做到这一点?
java ×9
maven ×3
file ×2
spring-mvc ×2
angularjs ×1
classloader ×1
eclipse ×1
exception ×1
java-ee ×1
jax-rs ×1
jaxb ×1
jersey ×1
jpa ×1
json ×1
oop ×1
server-push ×1
servlet-3.0 ×1
servlets ×1
spring ×1
throw ×1
xml ×1
xmladapter ×1