您好我在Swift中使用带有Alamofire的Object Mapper,我正在尝试将枚举原始值映射到真实的Enum.
这是我的枚举,也是我试图在函数映射中使用的代码.你能帮我解决一下EnumTransform的参数或如何修改代码吗?我知道我可以将值读作字符串并使用LevelType(rawValue:stringValue).
提前致谢.
enum LevelType : String {
case NEW = "NEW"
case UPDATE = "UPDATE"
}
func mapping(map: Map) {
typeEnum <- (map[“type”], EnumTransformable(???) )
}
Run Code Online (Sandbox Code Playgroud) 我有一个java.time.Instant
创建数据字段的实体:
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode
public class Item {
private String id;
private String url;
private Instant createdDate;
}
Run Code Online (Sandbox Code Playgroud)
我com.fasterxml.jackson.databind.ObjectMapper
用来将项目保存为Elasticsearch作为JSON:
bulkRequestBody.append(objectMapper.writeValueAsString(item));
Run Code Online (Sandbox Code Playgroud)
ObjectMapper
将此字段序列化为对象:
"createdDate": {
"epochSecond": 1502643595,
"nano": 466000000
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试注释,@JsonFormat(shape = JsonFormat.Shape.STRING)
但它对我不起作用.
我的问题是如何将此字段序列化为2010-05-30 22:15:52
字符串?
我有一个用例,我在请求中收到一些属性,如下所示,
"filters": [
{
"field": "fName",
"value": "Tom"
},
{
"field": "LName",
"value": "Hanks"
}
]
Run Code Online (Sandbox Code Playgroud)
我没有为此定义模型.我只是在请求中收到这些属性,并使用这些属性触发弹性搜索查询.我在弹性搜索中的记录具有相同的属性名称.
现在,我必须支持遗产应用程序,其中属性的名称完全不同.例如:fName变为firstName,lName变为lastName.
问题:需要在请求中接受旧的属性名称,将它们转换为新的属性名称,以便它与我的弹性搜索记录匹配.获取具有新属性名称的数据,并在从应用程序发出响应之前转换回旧属性.
注意:我没有为这些记录定义POJO.
如何有效实现这一目标?我正在考虑使用Orika mapper,但不确定如果不首先定义类,它将如何工作.
我正在升级项目的版本,目前正在使用 jackson-databind-2.13.0 。但我注意到 ObjectMapper 的启用方法已被弃用。
他们说要像这样使用它。
@deprecated Since 2.13 use {@code JsonMapper.builder().enable(...)}
Run Code Online (Sandbox Code Playgroud)
但我无法使用它。
下面是我的 ObjectMapper 实例创建代码。我该如何改变?
@Bean(name = {"objectMapper"})
@Primary
ObjectMapper objectMapper() {
return newObjectMapper();
}
public static ObjectMapper newObjectMapper() {
ObjectMapper objectMapper =
new ObjectMapper()
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true)
.setSerializationInclusion(JsonInclude.Include.NON_NULL)
.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES);
JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addSerializer(OffsetDateTime.class, new OffsetDateTimeSerializer());
javaTimeModule.addDeserializer(OffsetDateTime.class, new OffsetDateTimeDeserializer());
javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer());
javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer());
objectMapper
.registerModule(javaTimeModule)
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
return objectMapper;
Run Code Online (Sandbox Code Playgroud)
}
解决方案:
ObjectMapper objectMapper = JsonMapper
.builder()
.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES)
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, …
Run Code Online (Sandbox Code Playgroud) 如何将String映射到List和List to String?
考虑一下我们跟随classess
class People{
private String primaryEmailAddress;
private String secondaryEmailAddress;
private List<String> phones;
//getter and setters
}
class PeopleTO{
private List<String> emailAddress;
private String primaryPhone;
private String secondaryPhone;
//getter and setters
}
Run Code Online (Sandbox Code Playgroud)
在Dozer和Orika中,我们可以使用以下代码行轻松映射
fields("primaryEmailAddress", "emailAddress[0]")
fields("secondaryEmailAddress", "emailAddress[1]")
fields("phones[0]", "primaryPhone")
fields("phones[1]", "secondaryPhone")
Run Code Online (Sandbox Code Playgroud)
我如何在MapStruct中进行相同类型的映射?我会在哪里找到有关mapstruct的更多示例?
我使用 Java 11 并希望将 LocalDate/LocalDateTime 序列化/反序列化为字符串。好的。我添加了依赖:
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>${jackson.version}</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
和模块:
@Bean
public ObjectMapper objectMapper() {
return new ObjectMapper()
.registerModule(new JavaTimeModule())
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
}
Run Code Online (Sandbox Code Playgroud)
当我将日期发送到我的应用程序时,它会正确反序列化:
{"profileId":12608,"birthDate":"2008-03-20","relativeType":"SON","cohabitants":true}
Run Code Online (Sandbox Code Playgroud)
当我直接使用 objectMapper 作为 bean 时,它也可以正确序列化:
{"code":"SUCCESS","id":868,"profileId":12608,"birthDate":"2008-03-20","relativeType":"SON","cohabitants":true}
Run Code Online (Sandbox Code Playgroud)
但是当它与控制器序列化时,它序列化为数组:
{"code":"SUCCESS","id":868,"profileId":12608,"birthDate":[2008,3,20],"relativeType":"SON","cohabitants":true}
Run Code Online (Sandbox Code Playgroud)
问题是在控制器上反序列化正文中的日期。控制器是:
@PostMapping
public Relative create(@Validated(Validation.Create.class) @RequestBody Relative relative) {
return service.create(relative);
}
Run Code Online (Sandbox Code Playgroud)
相对类:
@Getter
@Setter
@ToString(callSuper = true)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class Relative extends MortgageResponse {
@Null(groups = Validation.Create.class)
@NotNull(groups = Validation.Update.class)
private Long id;
@NotNull
private Long profileId;
private LocalDate birthDate;
private RelativeType relativeType; …
Run Code Online (Sandbox Code Playgroud) 我正在使用Moya创建一个API层,并.updateMyWeightGoal
在创建端点请求时不断为目标获取上述错误.
goalAPI.request(target: .updateMyWeightGoal(weightGoalData: goalInfo), success: { (response) in
//
}){ (response: [String : Any]) in
print(response)
}
Run Code Online (Sandbox Code Playgroud)
我已经创建了另一个相同类型的Moya API,并使用相同的方式调用它goalAPI
,它工作正常.
可能导致此问题的任何想法
这里参考的是weightGoalData
类型的类定义
class UpdatedWeightGoalInfo: Mappable {
var consumerUserID: Int?
var height: String?
var weight: String?
var goalWeight: String?
init() {
}
convenience init(userId: Int, weightGoalData: WeightGoalResponse) {
self.init()
consumerUserID = userId
height = "\(weightGoalData.currentHeight)"
weight = "\(weightGoalData.currentWeight)"
goalWeight = "\(weightGoalData.goalWeight)"
}
required init?(map: Map) {
}
func mapping(map: Map) {
consumerUserID <- map["consumerUserId"]
height <- …
Run Code Online (Sandbox Code Playgroud) 我正在使用AlamofireObjectMapper来解析对我的对象的json响应.AlamofireObjectMapper是ObjectMapper的扩展.
根据他们的文件,我的模型类必须符合Mappable
协议.例如:
class Forecast: Mappable {
var day: String?
var temperature: Int?
var conditions: String?
required init?(_ map: Map){
}
func mapping(map: Map) {
day <- map["day"]
temperature <- map["temperature"]
conditions <- map["conditions"]
}
}
Run Code Online (Sandbox Code Playgroud)
为了符合Mappable protocl,我的模型类必须为每个字段实现所需的初始化器和映射函数.这说得通.
但是,它如何支持struct
类型?例如,我有一个Coordinate
结构,我尝试符合Mappable
协议:
struct Coordinate: Mappable {
var xPos: Int
var yPos: Int
// ERROR: 'required' initializer in non-class type
required init?(_ map: Map) {}
func mapping(map: Map) {
xPos <- map["xPos"] …
Run Code Online (Sandbox Code Playgroud) 我是Xcodeprojects中使用pod的初学者.我被pod file.lock弄糊涂了.我想将框架"OBJECTMAPPER"更新为2.0以使其与swift 3兼容.但它不会.
为什么不更新到最新版本(2.0).它与pod file.lock有什么关系吗?如下 - >
为什么用它?
我找不到任何关于 jackson 的 ObjectMapper 与其他映射器(如 dozer/mapStruct/modelMapping/等)之间差异的解释。所有的文章都比较了 dozer/mapStruct/modelMapping 但他们忽略了 ObjectMapper。我不明白有什么问题?是同一个映射器吗?