我是一名Java程序员,对企业界不熟悉.最近我使用Groovy和Java 开发了一个应用程序.我编写的所有代码都使用了相当多的静态代码.高级技术部门要求我减少使用的静力学数量.我用Google搜索了同样的东西,我发现很多程序员都反对使用静态变量.
我发现静态变量使用起来更方便.而且我认为它们也是有效的(如果我错了,请纠正我),因为如果我必须对一个类中的函数进行10,000次调用,我很乐意将该方法设置为静态并使用简单的方法Class.methodCall()而不是用10,000个类的实例来混乱内存,对吧?
此外,静态减少了代码其他部分的相互依赖性.他们可以充当完美的国家持有者.除此之外,我发现静态在一些语言中得到广泛实现,如Smalltalk和Scala.那么为什么程序员(尤其是Java世界)中普遍存在对静态的压迫呢?
PS:如果我对静力学的假设是错误的,请纠正我.
请告诉代码示例为什么SimpleDateFormat不是线程安全的.这堂课有什么问题? SimpleDateFormat的格式功能有问题吗?请给出一个在课堂上演示此错误的代码.
FastDateFormat是线程安全的.为什么?b/w SimpleDateFormat和FastDateFormat有什么区别?
请用解释此问题的代码解释一下?
我对ObjectMapper如何工作以及在我的应用程序中的一般用途感到满意.我想了解的是实现ObjectMapper的最佳方法,以确保它被重用,而我不是在我的应用程序中创建不必要的实例?
我的想法是我可以在Utils类中声明ObjectMapper,如下所示:
public class Utils {
public final static ObjectMapper mapper = new ObjectMapper();
}
Run Code Online (Sandbox Code Playgroud)
然后我可以从我需要使用代码的各个地方引用它,例如:
JsonSimple jsonSimple = Utils.mapper.readValue(jsonString, JsonSimple.class);
Run Code Online (Sandbox Code Playgroud)
我遇到了另一个问题(我应该将Jackson的ObjectMapper声明为一个静态字段吗?),这促使了我的方法.我想也许关键的区别在于我想在多个不同的类中共享我的ObjectMapper实例,而不仅仅是在一个类中.
这种方法听起来合适还是我错过了什么?
谢谢
我有
@RequestMapping(method = RequestMethod.GET)
@ResponseBody
SessionInfo register(UserProfile profileJson){
...
}
Run Code Online (Sandbox Code Playgroud)
我以这种方式传递profileJson:
http://server/url?profileJson={"email": "mymail@gmail.com"}
Run Code Online (Sandbox Code Playgroud)
但我的profileJson对象具有所有空字段.我该怎么办才能让春天解析我的json?
Mapper实例是完全线程安全的,不需要创建单独使用的mapper,但mapper的配置可以更改。
虽然ObjectMapper具有复制功能,可以根据现有的映射器复制自定义配置,但如果我共享一个映射器,则不能保证当有人想要自定义映射器时,他们会复制共享的映射器。所以我想要一个不可变的映射器来共享,如果有人不小心更改了共享映射器,则应该抛出一些异常。
有这样的事吗?
我有一个包含多个@RestController类的 Spring Boot Web 应用程序。我喜欢我的 REST 控制器返回的默认 json 格式。
为了在我的 DAO bean(执行 json 序列化和反序列化)中使用,我创建了一个自定义ObjectMapper:
@Configuration
public class Config{
@Bean
public ObjectMapper getCustomObjectMapper() {
final ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.setPropertyNamingStrategy(new PropertyNamingStrategy.SnakeCaseStrategy());
return objectMapper;
}
}
Run Code Online (Sandbox Code Playgroud)
在我的每个 DAO 类中,我都会自动装配我的自定义ObjectMapper:
@Repository
@Transactional
public class MyDaoImpl implements MyDao {
@Autowired
ObjectMapper objectMapper
//Dao implementation...
}
Run Code Online (Sandbox Code Playgroud)
这一切正常。问题是我的自定义ObjectMapper被 Spring 自动获取并用于序列化 REST 响应。
这是不可取的。对于 REST 控制器,我想保留ObjectMapperSpring 默认创建的。
我如何告诉 Spring Boot不检测也不将我的自定义 …
我想从java字节数组中读取一个JSON"树",并使用Jackson将JSON"树"作为java字节数组写回.一种方法如下所示:
ObjectMapper om = new ObjectMapper();
JsonNode old = om.createObjectNode();
byte[] arr = om.writeValueAsBytes(old);
JsonNode new = om.readTree(arr);
Run Code Online (Sandbox Code Playgroud)
但是,杰克逊最近建议使用ObjectReader和ObjectWriter而不是ObjectMapper,因为配置中的线程安全,但也因为可能只与它们相关的优化.但是,ObjectReader不支持直接使用字节数组的readTree,而writeValueAsBytes比writeTree更通用,因此可能有某种方式(和理由)以某种方式跳过类型映射逻辑.
那么,今天,使用最新的Jackson(2.5),进行这两次转换的最快/最佳/推荐方式是什么?
我需要将以下 JSON 转换为 Java 对象。该物业providerResponse在JSON包含的属性的地图,但他们逃脱了,并包裹在双引号。因此,它不会将属性反序列providerResponse化为 Java 对象(它作为String)。我objectMapper.readValue(msgStr, classType)用来反序列化 JSON。该消息由 AWS 生成,用于 SNS 传送状态通知,我无法控制更改 JSON 消息。是否可以配置ObjectMapper为取消转义属性并反序列化为 Java 对象而不是String?
{
"delivery":{
"providerResponse":"{\"sqsRequestId\":\"308ee0c6-7d51-57b0-a472-af8e6c41be0b\",\"sqsMessageId\":\"88dd59eb-c34d-4e4d-bb27-7e0d226daa2a\"}"
}
}
@JsonProperty("providerResponse")
private String providerResponse;
Run Code Online (Sandbox Code Playgroud) 我们正在使用对象映射器。当将 ObjectMapper 与 RowMapper 一起使用时,是否应该在每个 mapRow 内部(如下所示)声明它,还是在 mapRow 外部声明为类公共成员?我认为根据本文,它应该作为公共类成员在外部。我应该将 Jackson 的 ObjectMapper 声明为静态字段吗?
目前使用 Spring boot 和 SQL Server 数据库。研究数千/数百万 SQL 行的线程安全性。
List<Product> productList = namedParameterJdbcTemplate.query(sqlQuery,
parameters,
new ProductMapper(productRequest));
public class ProductMapper implements RowMapper<Product> {
@Override
public Product mapRow(ResultSet rs, int rowNum) throws SQLException {
ObjectMapper objectMapper = new ObjectMapper()
Product product = new Product();
product.setProductId(rs.getLong("ProductId"));
product.setProductType(rs.getString("ProductType"));
product.setLocations(objectMapper.readValue(rs.getString("Locations"), new TypeReference<List<ServiceLocation>>(){}));
} catch (Exception e) {
throw new ServiceException(e);
}
}
Run Code Online (Sandbox Code Playgroud)
注意:请不要问我们为什么要使用 ObjectMapper 编写这个手动映射器,我们正在做遗留编码,并且架构师要求这样做。
每个人!
我知道 SimpleDateFormat 不是线程安全的,我们不应该在多线程环境中使用单个实例!
ObjectMapper 是线程安全的,这很好。
现在我想知道在 ObjectMapper 中使用 SimpleDateFormat 是否安全!这是我的自定义 ObjectMapper:
public class MyObjectMapper extends ObjectMapper {
public MyObjectMapper() {
configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
}
}
Run Code Online (Sandbox Code Playgroud)
我在 springmvc 的配置中使用它。
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
jsonConverter.setObjectMapper(new MyObjectMapper());
converters.add(jsonConverter);
}
Run Code Online (Sandbox Code Playgroud)
如果是,ObjectMapper 是如何做到的!
如果不是,我应该使用什么 DateFormat !
谢谢!
我想从对象创建 JSON 字符串。
ObjectMapper om = new ObjectMapper();
String str = om.writeValueAsString(obj);
Run Code Online (Sandbox Code Playgroud)
有些对象很大,创建 JSON 字符串需要很长时间。创建 8MB JSON 字符串大约需要 15 秒。
我该如何改进这个?
假设我只想要ObjectMapper对象的普通实例。将其声明为bean有什么好处?
@Bean
public ObjectMapper objectMapper() {
return new ObjectMapper();
}
Run Code Online (Sandbox Code Playgroud)
为什么不每当我们需要时就制作一个新ObjectMapper的new ObjectMapper()?
还是将其声明为静态对象?
private static final ObjectMapper mapper = new ObjectMapper();
Run Code Online (Sandbox Code Playgroud) 我的班级看起来像这样:
public Class A {
private static final ObjectMapper mapper = new ObjectMapper();
public String get(String key) {
JsonNode nod = mapper.readTree(getFromCache(key));
}
}
Run Code Online (Sandbox Code Playgroud)
多个线程将访问此方法.我需要同步它吗?我是否需要在方法中移动映射器?
java ×13
jackson ×8
json ×7
static ×3
spring-boot ×2
spring-mvc ×2
non-static ×1
oop ×1
spring ×1