在我的职业生涯中,我看到了几个不同的设计,如何使用DAO,服务,控制器层.我想问一下两个相似但差别不大的两个.第一种设计在层之间产生可见屏障.控制器始终使用服务而仅使用服务.服务可以使用其他服务或DAO.控制器不能直接使用任何DAO.这个设计很清楚,但对我来说有很大的不足:我们总是要为DAO中的每个方法创建服务方法.但在很多情况下,Service方法只调用DAO方法和其他任何东西.
例如:我们有 DAO
class UserDao {
public List<User> findByName(String name) { ... }
public List<User> findByLastName(String name) { ... }
public List<User> findOlderThan(int age) { ... }
......
}
Run Code Online (Sandbox Code Playgroud)
控制器使用上述所有方法.在我们的案例中我们应该做些什么?创建类似的方法Service:
class UserService {
public List<User> findByName(String name) { return userDao.findByName(name); }
public List<User> findByLastName(String name) { return userDao.findByLastName(name); }
public List<User> findOlderThan(int age) { return userDao.findOlderThan(age); }
......
}
Run Code Online (Sandbox Code Playgroud)
对我来说,这是只读方法的额外不必要的层.
在第二种设计中,我们没有遇到任何问题,因为我们可以在控制器中直接使用DAO.在这个设计中我们有一个规则:如果我们想从"数据存储"中检索数据,我们可以在任何层使用DAO,如果我们想在"数据存储"中进行一些更改,我们必须使用服务方法.
伙计们,对于这些设计你怎么看?哪个更好?我应该在我的项目中使用哪些应该被遗忘?你能和我分享你在这方面的经验吗?
当类型名称太长时,在C#中我可以创建如下别名:
using Dict = System.Collections.Generic.Dictionary<string, string>;
Run Code Online (Sandbox Code Playgroud)
我可以像这样使用它:
Dict d = new Dict();
d.Add("key", "value");
Run Code Online (Sandbox Code Playgroud)
我可以在Java中创建一个与此类似的别名吗?
为简单起见,这是一个简单的类:
class GetterMethodsObject {
int id = 10;
public int getId() {
return id;
}
// @JsonIgnore
public boolean isId() {
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
序列化此对象应该给出:
{"id":10}
Run Code Online (Sandbox Code Playgroud)
因为有公共吸气方法.用法示例:
mapper=new ObjectMapper();
mapper.writeValueAsString(object);
Run Code Online (Sandbox Code Playgroud)
但我得到例外:
com.fasterxml.jackson.databind.JsonMappingException:
Conflicting getter definitions for property "id": org.citi.facility.GetterMethodsObject#isId(0 params) vs org.citi.facility.GetterMethodsObject#getId(0 params)
Run Code Online (Sandbox Code Playgroud)
由于id是Integer这样,我期待Jackson调用getId()方法,但不是isId().isId()只有当id是boolean时才应该调用方法?即使我@JsonIgnore说它没有帮助.我不能改变实际的对象.如何解决这个问题?
我收到以下错误消息,有人可以帮忙或建议如何最好地调试它。
无法
java.lang.String在 [Source: (PushbackInputStream);反序列化超出 START_OBJECT 令牌的实例;行:1,列:37610](通过参考链:com.model.ProductList["products"]->java.util.ArrayList[23]->com.model.Product["price"]->com.Price [“现在”])
我正在尝试从 REst API 调用反序列化 Products 对象。代码一直运行良好,直到我添加了反序列化 Price 子类的代码。这看起来如下,
"price": {
"was": "",
"then1": "",
"then2": "",
"now": "59.00",
"uom": "",
"currency": "GBP"
},
Run Code Online (Sandbox Code Playgroud)
我的价格 pojo 如下所示,
public class Price {
@JsonProperty("was")
String was;
@JsonProperty("then1")
String then1;
@JsonProperty("then2")
String then2;
@JsonProperty("now")
String now;
@JsonProperty("uom")
String uom;
@JsonProperty("currency")
String currency;
public Price() {
//blank constructor for JSON
}
@Override
public String toString() {
return "Price{" +
"was='" + was + '\'' +
", …Run Code Online (Sandbox Code Playgroud) I have POJO like where I have a BigDecimal field:
public class Foo implements Serializable {
private BigDecimal amount;
}
Run Code Online (Sandbox Code Playgroud)
I want to have the BigDecimal value up to 2 decimal place only. Is there any annotation exist using which I can directly change it's value at the field level itself? I cannot change it's type as well.
ObjectMapper尽管它可以通过 getter在应用程序内完成。
java bigdecimal jackson deserialization json-deserialization
我有以下输入JSON:
{
"id": "2ef8a2ee-054f-4b43-956a-8aa4f51a41d5",
"type": "VOICE",
"tags": [
{
"id": "some id 1",
"description": "some description 1"
},
{
"id": "some id 2",
"description": "some description 2"
}
],
"transcription": {
"key1": "val1",
"key2": "val2"
}
}
Run Code Online (Sandbox Code Playgroud)
但是,输出JSON应该类似,并且仅添加默认值:
{
"id": "2ef8a2ee-054f-4b43-956a-8aa4f51a41d5",
"created": "2019-06-18T18:12:37",
"firstName": "Khusan",
"lastName": "Sharipov",
"status": "OPEN"
"type": "VOICE",
"tags": [
{
"id": "some id 1",
"description": "some description 1"
},
{
"id": "some id 2",
"description": "some description 2"
}
],
"transcription": { …Run Code Online (Sandbox Code Playgroud) 我有CSV需要解析的文件。该文件的架构如下:
姓名、联系人 - 其中联系人是每个人的许多字符串,其中该联系人列的数量不规则。
例如:
john, john@wick.com, 123 123 123, fb/john.wick
mike, 123 0303 11
dave,
Run Code Online (Sandbox Code Playgroud)
我正在尝试为我的 bean创建一个CsvSchemawith :Jacskon CSV
public class Person {
private String name;
private String[] contacts;
}
Run Code Online (Sandbox Code Playgroud)
通过创建自定义架构:
CsvSchema schema = CsvSchema.builder()
.addColumn("name")
.addArrayColumn("contacts", ",")
.build();
Run Code Online (Sandbox Code Playgroud)
但我得到这个:
com.fasterxml.jackson.dataformat.csv.CsvMappingException: Too many entries: expected at most 2
Run Code Online (Sandbox Code Playgroud)
如何解决Jackson CSV这样的问题?
Java代码:
CsvMapper mapper = new CsvMapper();
CsvSchema schema = CsvSchema.builder()
.addColumn("name")
.addArrayColumn("contacts", ",")
.build();
MappingIterator<Person> it = mapper.readerFor(Person.class).with(schema)
.readValues(csvString);
List<Person> all …Run Code Online (Sandbox Code Playgroud) 如何阅读下面JSON使用Jackson ObjectMapper?我已经开发了代码,但出现以下错误。
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList` out of START_OBJECT token
at [Source: (File); line: 7, column: 19] (through reference chain: com.example.demo.resources.Orgnization["secondaryIds"])
Run Code Online (Sandbox Code Playgroud)
JSON
{
"id": "100000",
"name": "ABC",
"keyAccount": false,
"phone": "1111111",
"phoneExtn": "11",
"secondaryIds": {
"ROP": [
"700010015",
"454546767",
"747485968",
"343434343"
],
"AHID": [
"01122006",
"03112001"
]
}
}
Run Code Online (Sandbox Code Playgroud) 必须有一种方法来创建一个JavaTypefrom String.class?
注意:该方法的输入必须 JavaType适用于我的用例,因为该值是使用TypeFactory.
/** Returns a JavaType for Map<String, valueType> **/
private static JavaType stringToJavaType(JavaType valueType) {
TypeFactory typeFactory = objectMapper.getTypeFactory();
// this does not compile, can't mix Class and JavaType
return typeFactory.constructMapType(Map.class, String.class, valueType);
}
Run Code Online (Sandbox Code Playgroud)
如果我可以回答一个相关的问题,constructMapTypeover 有constructParametricType什么好处?
多年来,我一直在使用 Jackson 来序列化/反序列化对象,并且总是发现使用TypeReference<T>反序列化List等不必要地复杂。我创建了一个简单的辅助函数:
public static <T> TypeReference<List<T>> list() {
return new TypeReference<List<T>>(){}
}
Run Code Online (Sandbox Code Playgroud)
预期用途:
List<Foo> foos = objectMapper.readValue(json, list());
Run Code Online (Sandbox Code Playgroud)
它有效!的种类。通过调试器进行检查时,不是 的列表Foo,而是 的列表LinkedHashMap。我知道ObjectMapper反序列化为LinkedHashMap类型Object,我在这里阅读了解释:
但是,为什么它能够分配List<LinkedHasMap>给 a List<Foo>?至少不应该是某种ClassCastException吗?
另外,有没有办法用Java的类型系统来做到这一点?
注意:以下方法声明具有相同的问题,这是有道理的,因为不需要T确定附加参数:
public static <T> TypeReference<List<T>> listOf(Class<T> ignored) {
return new TypeReference<List<T>>(){}
}
Run Code Online (Sandbox Code Playgroud) java ×7
jackson ×6
json ×3
class ×2
alias ×1
apache-nifi ×1
bigdecimal ×1
coding-style ×1
csv ×1
fasterxml ×1
jackson2 ×1
jolt ×1
list ×1
objectmapper ×1
oop ×1
schema ×1
spring-boot ×1
type-erasure ×1