我尝试使用XMLMapper将一些配置类序列化为xml配置文件.但是我对属性生成有些麻烦.实际上生成的XML是完美的,但XMLMapper有时会为我的属性名称添加前缀.
例如
<Config zdef-2031720317:value="0">
Run Code Online (Sandbox Code Playgroud)
代替
<Config value="0">
Run Code Online (Sandbox Code Playgroud)
这真的很糟糕,因为我不能再用XOM处理xml结构了:(
这种效果来自哪里?我发现xml生成器似乎会自动修复命名空间以使属性唯一.为什么这是必要的,我该如何避免呢?
我在使RestEasy(3.0.10.Final)将路径参数解析为枚举值时遇到问题.
有枚举定义......
package com.stines;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonValue;
public enum MyNumber {
One("number-one"), Two("number-two");
@JsonIgnore private final String text;
@JsonIgnore
private MyNumber(final String text) {
this.text = text;
}
@JsonValue
public String getText() {
return text;
}
@JsonCreator
public static MyNumber byText(final String text) {
for (final MyNumber value : MyNumber.values()) {
if (value.getText().equals(text)) return value;
}
throw new IllegalArgumentException("Unknown number");
}
}
Run Code Online (Sandbox Code Playgroud)
......和终点......
@PUT
@Path("{number}")
void putNumber(
@PathParam("number") MyNumber number
);
Run Code Online (Sandbox Code Playgroud)
......我希望能打到PUT http://my-server/number-one.
我看到以下情况:
Caused …Run Code Online (Sandbox Code Playgroud) 我试图弄清楚如何使用jackson fastxml更改根节点名称。
例如:
public class Car {
@JsonProperty("engine-type")
String engineType = "v8";
}
public class Ford extends Car {
}
Ford car = new Ford();
ObjectMapper xmlMapper = new XmlMapper();
System.out.println(xmlMapper.writeValueAsString(this));
Run Code Online (Sandbox Code Playgroud)
结果是:
<Ford><engine-type>v8</engine-type></Ford>
Run Code Online (Sandbox Code Playgroud)
这就是我要的:
例如:
<car><engine-type>v8</engine-type></car>
Run Code Online (Sandbox Code Playgroud)
谢谢
Link.java
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({ "rel", "href","method" })
public class Link {
@JsonProperty("rel")
private String rel;
@JsonProperty("href")
private String href;
@JsonProperty("method")
private Method method;
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}
Run Code Online (Sandbox Code Playgroud)
我有这个第三方课程,带有fastxml jackson注释.我可以使用指定的toString()方法将给定对象转换为字符串.有没有办法使用该String来获取Link类型的对象?
注意:对象本身有一个嵌入对象(有几个嵌入对象),这些对象也需要从字符串本身转换为Method对象.
我有一个带有“描述”字段的对象列表。该字段可能很大(+3000个字符),但是我在预览中使用了该字段(我只显示100个首字符)。
杰克逊中是否有办法限制写入时String的大小?我只想让杰克逊将其裁剪为100个字符。(这里不需要进行bean验证)。
例如,如果我有一个这样的对象:
{
"description" : "bla bla bla bla... + 3000 char"
}
Run Code Online (Sandbox Code Playgroud)
理想情况下,我希望将其裁剪成小偷:
{
"description" : bla bla [max 100 chars] bla..."
}
Run Code Online (Sandbox Code Playgroud)
谢谢。
我有以下XML,我想反序列化为Java POJO.
<testdata>
<foo>
<bar>
<![CDATA[MESSAGE1]]>
</bar>
<bar>
<![CDATA[MESSAGE2]]>
</bar>
<bar>
<![CDATA[MESSAGE3]]>
</bar>
</foo>
</testdata>
Run Code Online (Sandbox Code Playgroud)
我有以下Java类
public class TestData {
@JacksonXmlProperty(localName = "foo")
private Foo foo;
public Foo getFoo() {
return foo;
}
public void setFoo(Foo foo) {
this.foo = foo;
}
}
Run Code Online (Sandbox Code Playgroud)
我有另一个类,如下
public class Foo {
@JacksonXmlProperty(localName = "bar")
@JacksonXmlCData
private List<String> barList;
public List<String> getBarList() {
return barList;
}
public void setBarList(List<String> barList) {
this.barList = barList;
}
}
Run Code Online (Sandbox Code Playgroud)
现在,当我使用下面的类运行代码时,我得到一个异常
private void readXml() throws FileNotFoundException, IOException { …Run Code Online (Sandbox Code Playgroud) 比方说,我有一堂课
public class A{
private UUID typeId;
private B data;
}
public abstract class B{
private String a;
}
public class BChildOne extends B{
... some variables
}
public class BChildTwo extends B{
... some variables
}
Run Code Online (Sandbox Code Playgroud)
B类的type根据A的typeId在变化,所以如果A的typeId为“XXX”,则数据字段类型为BChildOne,如果A的typeId为“YYY”,则数据字段类型为BChildTwo。
我怎样才能做到这一点?
所以我试过了;
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY, getterVisibility =
JsonAutoDetect.Visibility.NONE, setterVisibility =
JsonAutoDetect.Visibility.NONE)
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include =
JsonTypeInfo.As.EXTERNAL_PROPERTY , property = "typeId")
@JsonSubTypes({
@JsonSubTypes.Type(value = BChildOne.class, name = "40ad2fe6-e672-4f0e-
986e-
619c7a1a3223") }
)
public abstract class B{
Run Code Online (Sandbox Code Playgroud)
但我遇到了以下错误;
意外的标记 (END_OBJECT),预期的 …
public static <T> List<T> convertJSONStringTOListOfT(String jsonString, Class<T> t){
if(jsonString == null){
return null;
}
ObjectMapper mapper = new ObjectMapper();
try
{
List<T> list = mapper.readValue(jsonString, new TypeReference<List<T>>() {});
return list;
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
我有以上方法,当我尝试使用以下方法调用它时:
list = convertJSONStringTOListOfT(str, CustomAssessmentQuestionSetItem.class);
Run Code Online (Sandbox Code Playgroud)
返回的列表List<LinkedHashMap>不是List<CustomAssessmentQuestionSetItem>
虽然如果我不使用泛型,那么下面的代码工作正常:
list = mapper.readValue(str, new TypeReference<List<CustomAssessmentQuestionSetItem>>() {});
Run Code Online (Sandbox Code Playgroud)
这两个调用对我来说都是一样的.无法理解为什么通用的创建List<LinkedHashMap>而不是List<CustomAssessmentQuestionSetItem>
仅供参考:我也尝试将方法签名更改为
public static <T> List<T> convertJSONStringTOListOfT(String jsonString, T …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 jackson 2.6.3 将对象序列化为 json 我想在序列化的 json 中包含类型信息。这不适用于嵌套在此类中的成员。
这是测试代码。
public class Test {
@JsonSubTypes({ @JsonSubTypes.Type(value = ConcreteA.class)})
interface A {
}
@JsonTypeInfo( use=JsonTypeInfo.Id.CLASS)
class ConcreteA implements A {
}
@JsonSubTypes({ @JsonSubTypes.Type(value = ConcreteB.class)})
interface B {
}
@JsonTypeInfo( use=JsonTypeInfo.Id.CLASS)
class ConcreteB implements B {
A a;
public A getA() {
return a=new ConcreteA();
}
}
@org.junit.Test
public void testSerialisation() throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
System.out.println(objectMapper.writeValueAsString(new ConcreteB()));
}
}
Run Code Online (Sandbox Code Playgroud)
jackson 正在转换的 json 是
{"@class":"Test$ConcreteB","a":{}}
Run Code Online (Sandbox Code Playgroud)
请注意,它不包括字段“a”的类型信息。仅序列化 A 时确实包含类型信息。 …
我使用库rapidxml从我的服务器解析我的数据.几个小时后我开始收到此错误消息.当我尝试检索一些json.我不知道为什么我收到此消息,因为它之前工作正常.所以,如果有人能够解释我的意思,那就太棒了.先谢谢你!
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Duplicate creator property "id" (index 0 vs 1)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:270)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:245)
at com.fasterxml.jackson.databind.deser.DeserializerCache.findValueDeserializer(DeserializerCache.java:143)
at com.fasterxml.jackson.databind.DeserializationContext.findContextualValueDeserializer(DeserializationContext.java:406)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.createContextual(CollectionDeserializer.java:164)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.createContextual(CollectionDeserializer.java:25)
at com.fasterxml.jackson.databind.DeserializationContext.handleSecondaryContextualization(DeserializationContext.java:653)
at com.fasterxml.jackson.databind.DeserializationContext.findContextualValueDeserializer(DeserializationContext.java:408)
Run Code Online (Sandbox Code Playgroud)
我的班级:
@JsonIgnoreProperties(ignoreUnknown = true)
public class Dieser
implements Serializable
{
private static final long serialVersionUID = 8847499061681136159L;
@JsonProperty("id")
private long id;
@JsonProperty("nickName")
private String nickName;
@JsonProperty("photo")
private Photo photo;
@JsonProperty("mail")
private String mail;
@JsonProperty("password")
private String password;
@JsonProperty("dateTime")
private long dateTime;
@JsonProperty("dieseCount")
private int dieseCount;
@JsonProperty("activateAccount")
public boolean activateAccount;
@JsonProperty("activateDateTime")
public long …Run Code Online (Sandbox Code Playgroud) 我们在服务代码中将模型定义为-
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class SomeData {
public boolean tnAvailable;
@NonNull
public String sTempChange;
public boolean isTnAvailable() {
return faAvailable;
}
public void setTnAvailable(boolean faAvailable) {
this.faAvailable = faAvailable;
}
@Nonnull
public String getSTempChange() {
return sTempChange;
}
public void setSTempChange(@Nonnull String sTempChange) {
this.sTempChange = sTempChange;
}
}
Run Code Online (Sandbox Code Playgroud)
当查询包含上述模型的api作为响应时,我们得到的响应为-
"someData": {
"tnAvailable": true,
"stempChange": "trial_001"
}
Run Code Online (Sandbox Code Playgroud)
使我们感到惊讶的是stempChange(注意小写t)而不是sTempChange响应的属性。
在API调用期间对对象进行序列化和反序列化时,怀疑原因是Jackson com.fasterxml.jackson.core:jackson-core:2.5.2,因为我们没有使用任何其他getter-setter ot包装器来更改属性。为什么会发生这种情况,并且序列化/反序列化是寻找此结果的正确方向?
编辑 -从@Windle发表的评论中,尝试解释这里的不同之处。我再次重申: “尽管那里的问题在很大程度上与相同的情况有关。但是我也期待在fastxml中实现和编写文档的原因。”
我有一个具有duration类型数据成员的Topic 类LocalTime
@Entity
@Table(name= "topics")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class,
property = "id",
scope = Long.class)
@DynamicInsert(true)
@DynamicUpdate(true)
public class Topics implements Serializable {
private static final long serialVersionUID = -1777454701749518940L;
@Id
@Column(name= "id")
private Long id = Long.parseLong(UUID.randomUUID().toString().substring(0, 8), 16);
@NotEmpty
@NotNull
@Column(name= "topic", columnDefinition = "Text", length = 50000)
private String topic;
@Convert(converter = LocalTimeConverter.class)
private LocalTime duration;
// getters and setters
}
Run Code Online (Sandbox Code Playgroud)
输入json:
{
"topic":"x111",
"imageUrl":"http://x.com/x1/x11/x111",
"duration":"00:00:05"
}
Run Code Online (Sandbox Code Playgroud)
错误:
Can not construct instance of java.time.LocalTime: no …Run Code Online (Sandbox Code Playgroud) 我在 Kotlin 中有一个这样的(数据)类。
在使用活动 Spring 配置文件运行应用程序时,live我只想返回name.
但是运行具有活动 Spring 配置文件的应用程序test,我想version显示出来,这样我就可以测试我是否得到了正确的版本。
data class MyClass(
val name: String
@JsonIgnore
val version: Number
)
Run Code Online (Sandbox Code Playgroud)
有可能吗?