net.sf.json.JSONException:Struts 2中的层次结构中存在一个循环 [英] net.sf.json.JSONException: There is a cycle in the hierarchy in Struts 2
问题描述
我正在使用实现ModelDriven
的Struts类.我能够从jQuery传递数据并将数据保存在数据库中.
I am using Struts class which implements ModelDriven
. I am able to pass the data from jQuery and save the data on the database.
当我尝试取回数据并将其传递回jQuery时,我不确定为什么它在jQuery中不可用.我确定我的基本流程中缺少一些东西.
When I try to retrieve the data back and pass it back to jQuery, I am not sure why it's not available in jQuery. I am sure I am missing something with the basic flow..
这是我的动作课.
public HttpHeaders index() {
model = projectService.getProjectDetails(project.getUserID());
return new DefaultHttpHeaders("success").setLocationId("");
}
@Override
public Object getModel() {
return project;
}
public Project getProject() {
return project;
}
public void setProject(Project project) {
this.project = project;
}
这是我的jQuery
Here is my jQuery
function getProjectDetails() {
var userID = localStorage.getItem('userID');
var request = $.ajax({
url : '/SUH/project.json',
data : {
userID : userID
},
dataType : 'json',
type : 'GET',
async : true
});
request.done(function(data) {
console.log(JSON.stringify(data));
$.each(data, function(index, element) {
console.log('element project--->' + index + ":" + element);
});
});
request.fail(function(jqXHR, textStatus) {
console.log('faik');
});
}
Action类中的模型对象具有所有可用数据,但我尝试返回模型或项目对象,但两者均无效.
The model object in the Action class has all the data available but I tried to return model or project objects but both didn't work.
推荐答案
默认情况下,Struts2 REST插件正在使用 json- lib 用于序列化您的bean.如果您使用的是ModelDriven
,则它在处理结果时将直接访问您的模型.由于您在请求URL中使用扩展名.json
,因此将按扩展名选择内容类型处理程序.应该是JsonLibHandler
.
By default Struts2 REST plugin is using json-lib for serialization your beans. If you are using ModelDriven
then it accesses your model directly when it processes a result. Since you are using the extension .json
in the request URL the content type handler is selected by extension. It should be JsonLibHandler
.
如果obj
是数组或列表,则此处理程序正在使用JSONArray.fromObject(obj)
;否则,将使用JSONObject.fromObject(obj)
来获取可以序列化并写入响应的JSONObejct
.
This handler is using JSONArray.fromObject(obj)
if obj
is an array or list or JSONObject.fromObject(obj)
otherwise to get JSONObejct
that could be serialized and written to the response.
obj
是getModel()
返回的值,在您的情况下为project
.
The obj
is the value returned by getModel()
, in your case it will be project
.
因为JsonLibHandler
使用默认的JsonConfig
,所以除非它们是public
字段,否则不能从要序列化的Bean中排除属性.
Because JsonLibHandler
is using default JsonConfig
you can't exclude properties from the bean to be serialized unless they are public
fields.
JsonConfig
可以支持json-lib的以下功能:
The following features of json-lib could be powered with JsonConfig
:
- 循环检测,有两种默认策略(默认抛出 一个例外),您可以注册自己的
- 何时跳过瞬态字段 序列化为JSON(默认为不跳过)跳过JAP @Transient注释 序列化为JSON时的方法(默认=不跳过)
- 排除豆 序列化为JSON时的属性和/或映射键 (默认= ['class','metaClass','declaringClass'])
- 过滤器提供了 序列化为时排除/包含属性的更详细的信息 JSON或转换回Java
- Cycle detection, there are two default strategies (default throws an exception), you can register your own
- Skip transient fields when serailizing to JSON (default=don't skip) Skip JAP @Transient annotated methods when serailizing to JSON (default=don't skip)
- Exclude bean properties and/or map keys when serailizing to JSON (default=['class','metaClass','declaringClass'])
- Filters provide a finer detail for excluding/including properties when serializing to JSON or transforming back to Java
您可以找到此代码段,该代码段可以排除某些属性.
You can find this code snippets that allows you to exclude some properties.
排除属性
String str = "{'string':'JSON', 'integer': 1, 'double': 2.0, 'boolean': true}";
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setExcludes( new String[]{ "double", "boolean" } );
JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON( str, jsonConfig );
assertEquals( "JSON", jsonObject.getString("string") );
assertEquals( 1, jsonObject.getInt("integer") );
assertFalse( jsonObject.has("double") );
assertFalse( jsonObject.has("boolean") );
排除属性(带有过滤器)
String str = "{'string':'JSON', 'integer': 1, 'double': 2.0, 'boolean': true}";
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setJsonPropertyFilter( new PropertyFilter(){
public boolean apply( Object source, String name, Object value ) {
if( "double".equals(value) || "boolean".equals(value) ){
return true;
}
return false;
}
});
JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON( str, jsonConfig );
assertEquals( "JSON", jsonObject.getString("string") );
assertEquals( 1, jsonObject.getInt("integer") );
assertFalse( jsonObject.has("double") );
assertFalse( jsonObject.has("boolean") );
但是您可以选择使用自己的ContentTypeHandler
覆盖默认值.
But you have an option to use your own ContentTypeHandler
to override defaults.
替代方法是使用Jackson库来处理请求.如文档页面中所述:使用Jackson框架作为JSON ContentTypeHandler
.
The alternative is to use Jackson library to handle request. As described in the docs page: Use Jackson framework as JSON ContentTypeHandler
.
默认JSON内容处理程序构建在JSON-lib之上.如果 您更喜欢使用Jackson框架进行JSON序列化, 可以将
JacksonLibHandler
配置为json的内容处理程序 要求.
The default JSON Content Handler is build on top of the JSON-lib. If you prefer to use the Jackson framework for JSON serialisation, you can configure the
JacksonLibHandler
as Content Handler for your json requests.
首先,您需要将jackson依赖项添加到您的Web应用程序中
通过下载jar文件并将其放在WEB-INF/lib下或通过添加
在pom.xml
中的依赖项部分中跟随以下xml代码段
您正在使用maven作为构建系统.
First you need to add the jackson dependency to your web application
by downloading the jar file and put it under WEB-INF/lib or by adding
following xml snippet to your dependencies section in the pom.xml
when
you are using maven as build system.
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-jaxrs</artifactId>
<version>1.9.13</version>
</dependency>
现在您可以用Jackson内容覆盖Content Handler.
struts.xml
中的处理程序:
Now you can overwrite the Content Handler with the Jackson Content
Handler in the struts.xml
:
<bean type="org.apache.struts2.rest.handler.ContentTypeHandler" name="jackson" class="org.apache.struts2.rest.handler.JacksonLibHandler"/>
<constant name="struts.rest.handlerOverride.json" value="jackson"/>
<!-- Set to false if the json content can be returned for any kind of http method -->
<constant name="struts.rest.content.restrictToGET" value="false"/>
<!-- Set encoding to UTF-8, default is ISO-8859-1 -->
<constant name="struts.i18n.encoding" value="UTF-8"/>
之后,您可以使用@JsonIgnore
注释.
After that you can use @JsonIgnore
annotation.
这篇关于net.sf.json.JSONException:Struts 2中的层次结构中存在一个循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!