@GetMapping("item")
public @ResponseBody String get(@ModelAttribute Item item)
Run Code Online (Sandbox Code Playgroud)
Item 有属性
name
itemType
当我访问/item?name=foo&item_type=bar了item被只填充name 未用itemType.
我尝试了很多东西来获取itemType映射的属性item_type.
Item的itemType属性.这里描述.Item类级别添加了PropertyNamingStrategy .这里描述.我怎么解决这个问题?
顺便说一句.对于传入的不是传出的JSON序列化我只有这个问题Item.
更新04/24/17:
下面是一个最小的示例,用于演示问题:访问时/item您会看到"传出"的JSON序列化有效,但访问时/item/search?name=foo&item_type=bar它不适用于'传入'JSON反序列化.
项目
package sample;
import java.io.Serializable;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.PropertyNamingStrategy.SnakeCaseStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
@JsonNaming(SnakeCaseStrategy.class)
public class Item implements Serializable {
private String name; …Run Code Online (Sandbox Code Playgroud) 我正在制作一个使用Drools计划器的应用程序.
该@ValueRangeFromSolutionProperty假设指代属性从另一个类(NQueens在这种情况下).来自@ValueRangeFromSolutionProperty的JavaDocs:
propertyName
The property name of which exists a getter on the Solution that returns a Collection.
Run Code Online (Sandbox Code Playgroud)
但我注意到一个不一致的地方:注释器使用了rowList来自的属性NQueens.但是rowList(相反RowList)是一个私有变量(参见下面的片段).如果它应该通过内省推断出一个属性(来自它的getter和setter方法),那么它是不是应该拼写RowList为getRowList()?
问题: Java如何从getter方法推断(内省)属性名称(case和all)?
或者直接@ValueRangeFromSolutionProperty访问私有变量?
背景细节:From Queen.java,代表棋盘上的女王的类:
public class Queen extends AbstractPersistable {
....
@ValueRangeFromSolutionProperty(propertyName = "rowList")
public Row getRow() {
return row;
....
Run Code Online (Sandbox Code Playgroud)
从NQueens.java,@ValueRangeFromSolutionProperty获取它的属性的类来自:
public class NQueens extends AbstractPersistable implements …Run Code Online (Sandbox Code Playgroud) 有没有办法指定不同的序列化/反序列化 JSON 字段名称,而不必显式写出 getter 和 setter 方法,也许使用 lombok getter 和 setter?
与此示例类似,以下代码允许将传入的 JSON 反序列化为不同的 POJO 字段名称。它还导致 POJO 字段名称按原样序列化:
public class PrivacySettings {
private String chiefDataOfficerName;
@JsonProperty("CDO_Name__c")
private void setChiefDataOfficerName(String chiefDataOfficerName) {
this.chiefDataOfficerName = chiefDataOfficerName;
}
@JsonProperty("chiefDataOfficerName")
private String getChiefDataOfficerName() {
return chiefDataOfficerName;
}
}
Run Code Online (Sandbox Code Playgroud)
这看起来很冗长,但我无法让它与 @Getter 和 @Setter 一起使用。我确实看到 Jackson 支持 @JsonAlias,这在这个特定示例中可能会有所帮助,但我还需要使用不同的名称进行序列化。
看起来这应该很简单,比如:
@Getter
@Setter
public class PrivacySettings {
@JsonSetter("CDO_Name__c")
@JsonGetter("chiefDataOfficerName")
private String chiefDataOfficerName;
}
Run Code Online (Sandbox Code Playgroud)
但这当然是无效的。
jackson ×2
java ×1
javabeans ×1
json ×1
lombok ×1
naming ×1
spring ×1
spring-boot ×1
spring-mvc ×1