我正在调查使用FreeMarker编写EDI文件.这些基本上是格式化(和验证)的电子发票.我决定开始编写一个简单的例子,并且很难让它模板化'嵌套的pojo'.我的意思是一个包含POJO的POJO,其中两个都有我想要输出的数据.我写了一个单元测试(自包含),由于未知原因而失败.当我运行测试时,我得到以下异常:
Expression user.getSub is undefined on line 1, column 24 in simple.
The problematic instruction:
----------
==> ${user.getSub().user} [on line 1, column 22 in simple]
----------
Java backtrace for programmers:
----------
freemarker.core.InvalidReferenceException: Expression user.getSub is undefined on line 1, column 24 in simple.
at freemarker.core.TemplateObject.assertNonNull(TemplateObject.java:124)
at freemarker.core.TemplateObject.invalidTypeException(TemplateObject.java:134)
<snip>
example.TestFreeMarkerTemplating.testSimpleTemplate(TestFreeMarkerTemplating.java:23)
<snip>
Run Code Online (Sandbox Code Playgroud)
我在模板上尝试了很多变化,并没有取得成功.这是测试:
package example;
import java.io.*;
import org.junit.Test;
import freemarker.cache.StringTemplateLoader;
import freemarker.ext.beans.BeansWrapper;
import freemarker.template.*;
import static org.junit.Assert.*;
public class TestFreeMarkerTemplating {
Configuration cfg = new Configuration();
StringTemplateLoader stringLoader = new StringTemplateLoader(); …Run Code Online (Sandbox Code Playgroud) 在我们的一个项目中,我们使用java webapp与MongoDB实例进行通信.在数据库中,我们DBRefs用来跟踪一些对象关系.我们使用jackson(使用mongodb-jackson-mapper)使用POJO对象进行序列化.
但是,我们使用相同的POJO然后(de)序列化到外部世界,我们的前端处理呈现JSON.
现在,我们需要一种方法,让外部世界的序列化包含来自a的引用对象DBRef(以便UI可以呈现完整对象),而我们显然希望将数据DBRef写入数据库,而不是整个对象.
现在我写了一些未经测试的静态嵌套类代码:
public static class FooReference {
public DBRef<Foo> foo;
// FIXME how to ensure that this doesn't go into the database?
public Foo getFoo() {
return foo.fetch();
}
}
Run Code Online (Sandbox Code Playgroud)
理想情况下,我想要一种方法来注释这个,以便我可以(或)使用或不使用getFoo()结果序列化它,可能取决于一些配置对象.这可能吗?你有没有看到更好的方法来做这件事?
我知道使用@EJB注释的注入只能在EJB类,servlet或JSF托管bean中进行,但同时我需要在POJO类中有一个注入业务接口的实例,所以我想到做以下事情:
在我的JSF托管bean中
@EJB BusinessInterfaceLocal businessInterface;
private void someMethod(){
PojoInterface pojo = new PojoClass(this.businessInterface);
}
Run Code Online (Sandbox Code Playgroud)
在我的POJO类中,我有这个构造函数
BusinessInterfaceLocal businessInterface;
public PojoClass(BusinessInterfaceLocal businessInterface){
this.businessInterface = businessInterface;
//The following throws a Null Pointer Exception
this.businessInterface.someMethodCall();
}
Run Code Online (Sandbox Code Playgroud)
上述工作不应该正常吗?但事实并非如此,PojoClass中的businessInterface对象被计算为null,从而抛出空指针异常.
我希望有人能指出我,我做错了什么.
提前致谢.
我为什么要使用EJB?我能从中得到什么,我无法通过其他方式获得?
与POJO相比,使用EJB有什么好处?
我有一个包含枚举属性的POJO.
[Speed.java]
public class SpeedEntry implements Serializable {
[...]
private int idSpeed;
private SpeedStatus status; // enum Property
[...]
Run Code Online (Sandbox Code Playgroud)
[SpeedStatus.java]
public enum SpeedStatus {
[...]
VALID(1), INVALID(2), UNKNOWN(0); // Possible values.
private int value;
// Default constructor
private SpeedStatus(final int pValue) {
this.value = pValue;
}
[...]
Run Code Online (Sandbox Code Playgroud)
我希望存储和检索Speed对象,并像往常一样使用MyBatis填充其属性.分配给SpeedStatus的列创建为INT(11).
在这种情况下,执行INSERT非常直接访问其内部值属性:
#{status.value}
Run Code Online (Sandbox Code Playgroud)
但是,检索对象并从存储在数据库中的整数中获取其枚举值并不像插入它那么容易.没用,我尝试使用resultMap:
[speedMapper.xml]
<resultMap id="speedMap" type="Speed">
<result property="idSpeed" column="idSpeed" />
<result column="status" property="status.value"
typeHandler="org.apache.ibatis.type.EnumOrdinalTypeHandler"/>
</resultMap>
Run Code Online (Sandbox Code Playgroud)
那么...... 有可能实现我所期待的目标吗? 那边有简单的澄清例子吗?
什么是最好的选择?我应该将存储在数据库中的类型更改为"枚举"吗?
提前致谢.
任何人都知道如何/或可能 - 创建一个具有列特定顺序的表; 保存之前的配置顺序 - 例如在DB中,并在特定视图上上传?我也想知道如何从POJOS类 - bean生成这个列标题和内容.
有什么好主意吗?
我有几个不同的POJO使用一个生成器模式,但添加的助洗剂为每一个和生成后Object.toString,Object.hashCode和Object.equals,我的类最终会被大约100行代码.必须有一个更好的方法来处理这个问题.我认为拥有一些反思性的建设者会有很多帮助,但我不确定这是不是很好的做法,而且我也不确定我是如何做到这一点的.换句话说,有没有办法实现这样的构建器?
一个简单的POJO:
public class Foo {
public int id;
public String title;
public boolean change;
...
}
Run Code Online (Sandbox Code Playgroud)
然后某种反思建设者:
Foo = ReflectiveBuilder.from(Foo.class).id(1).title("title").change(false).build();
Run Code Online (Sandbox Code Playgroud) 我想比较两个对象的ArrayList,并根据对象中的id从第二个ArrayList中找到不匹配的值.
例如:
Person.java
private int id;
private String name;
private String place;
Run Code Online (Sandbox Code Playgroud)
MainActivity.java:
ArrayList<Person> arrayList1 = new ArrayList<Person>();
arrayList1.add(new Person(1,"name","place"));
arrayList1.add(new Person(2,"name","place"));
arrayList1.add(new Person(3,"name","place"));
ArrayList<Person> arrayList2 = new ArrayList<Person>();
arrayList2.add(new Person(1,"name","place"));
arrayList2.add(new Person(3,"name","place"));
arrayList2.add(new Person(5,"name","place"));
arrayList2.add(new Person(6,"name","place"));
Run Code Online (Sandbox Code Playgroud)
我想比较arrayList1,arrayList2,并需要从arrayList2中找到不匹配的值.我需要id值5,6.
我怎样才能做到这一点?
我只是在Android上为我的项目尝试Firebase.
我得到的问题是,每次拍摄快照并将其"投"回POJO我都会得到这个 -
"反序列化时映射,但得到了一个类java.util.ArrayList"异常.
我一直在环顾四周,甚至改变我所有的模型使用HashMap而没有任何ArrayList结果,仍然得到相同的结果.
这是我的模特:
public class DevicesController {
public DevicesCollection devices;
public int numberOfport;
String timerStatus;
public DevicesController(){
devices = new DevicesCollection();
timerStatus = "UPDATED";
}
public DevicesController(int numberOfport){
devices = new DevicesCollection();
this.numberOfport = numberOfport;
timerStatus = "UPDATED";
}
public ArrayList<Integer> getCurrentState(String in){
ArrayList<Integer> currentState = new ArrayList<Integer>();
for(int i = 0; i < numberOfport; i++){
currentState.add(0);
}
Iterator it = this.getDevices().getAllDevices().entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry) …Run Code Online (Sandbox Code Playgroud) 我想使用Java来学习graphQL。我想查询现有的graphQL服务。到目前为止,我发现从POJO类以JSON或IDL形式生成graphQL schemafile的方法,但反过来又不是。根据到目前为止的理解,我必须自己创建Java类,以“表示”我将从查询中接收到的对象。但是我要使用的服务有大量的端点和查询,我自己编写每个pojo类的需求听起来好像我错过了一些东西...我认为必须有一种方法可以像从REST API框架中生成我习惯的存根一样使用招摇或yaml文件?所以... 如何仅给定schemafile自动生成pojo类?我已经在graphql-java中阅读了关于模式优先方法的描述,但他们还假设自己编写pojo类。
谢谢