我正在尝试将Jackson2.0与Spring3.1一起使用,以便我可以使用jackson-Module-Hibernate。我已按照此处所述的步骤http://blog.pastelstudios.com/2012/03/12/spring-3-1-hibernate-4-jackson-module-hibernate/进行操作。所有这些似乎工作正常,但是当我尝试使用JSON视图以便JSON只包含视图中的字段时它不起作用。
活动视图始终为null。如何使视图处于活动状态?我已经尝试了一天没有运气......任何帮助都将非常感激。提前谢谢。
以下是相关代码。
这是Mapper
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.hibernate4.Hibernate4Module;
public class HibernateAwareObjectMapper extends ObjectMapper {
public HibernateAwareObjectMapper() {
Hibernate4Module hm = new Hibernate4Module();
registerModule(hm);
hm.configure(Hibernate4Module.Feature.FORCE_LAZY_LOADING, true);
}
}
这是视图类
public class DiffViews {
public static class Public { }
}
这是我使用视图的POJO
@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id")
@JsonIgnoreProperties(ignoreUnknown=true)
@Entity
public class Premium implements java.io.Serializable {
@JsonView(DiffViews.Public.class)
private String sequence;
@JsonView(DiffViews.Public.class)
@Column(name = "SEQUENCE", nullable = false, length = 4)
public String getSequence() {
return this.sequence;
}
public void setSequence(String sequence) {
this.sequence = sequence;
}
@Column(name = "NAME", nullable = false)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
)
在我的弹簧控制器中
@RequestMapping("/cartonPremium")
public void listAll(
@RequestParam("page") int page, @RequestParam("rows") int maxResults,
@RequestParam("sidx") String sortKey, @RequestParam("sord") String sortOrder, HttpServletResponse response) {
HibernateAwareObjectMapper mapper = new HibernateAwareObjectMapper();
mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
mapper.enable(SerializationFeature.WRITE_CHAR_ARRAYS_AS_JSON_ARRAYS);
mapper.disable(MapperFeature.DEFAULT_VIEW_INCLUSION );
JqgridResponse<Premium> gridResponse = new JqgridResponse<Premium>();
gridResponse.setRows(premiumList);
gridResponse.setRecords(""+premiumList.size());
gridResponse.setTotal(""+premiumList.size());
gridResponse.setPage(""+page);
try {
ObjectWriter objWriter= mapper.writerWithView(DiffViews.Public.class);
Class<?> xxx = mapper.getSerializationConfig().getActiveView();
objWriter.writeValue(response.getOutputStream(), gridResponse);
//mapper.writerWithView(DiffViews.Public.class).writeValue(response.getOutputStream(), gridResponse);
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
我的春季配置
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="com.creata.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="com.creata.json.HibernateAwareObjectMapper" />
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
答案 0 :(得分:0)
我终于找到了我的错误...我在一个名为JqgridResponse的包装器中返回我的域POJO,并且我没有在这个包装类的字段中添加@JsonView注释。所以一切都很好json视图不会被忽略。