嗨,这与以下帖子有关-
smartgwt listgrid RestDataSource not populating
尝试使用smartGWT和Spring3构建应用程序时遇到问题。使用杰克逊将返回对象转换为json。
从spring控制器返回到客户端的json响应错误(帖子末尾的完整堆栈跟踪):
16:52:27.094 [ERROR] [utilapp] 16:52:27.092:TMR1:WARN:RestDataSource:isc_ItemListGrid_1_0:RestDataSouce transformResponse():JSON响应文本似乎不是标准响应格式。
com.smartgwt.client.core.JsObject $ SGWT_WARN:16:52:27.092:TMR1:WARN:RestDataSource:isc_ItemListGrid_1_0:RestDataSouce transformResponse():JSON响应文本似乎不是标准响应格式。
只是想知道之前是否有人遇到过这样的错误,或者任何暗示其背后原因的提示将非常有用。
直接从浏览器点击相应的GET方法网址时的Json响应有效负载-
{“状态”:0,“数据”:null,“错误”:{},“ endRow”:0,“ startRow”:0,“ totalRows”:-1}
尽管在控制器中进行设置,但所有字段均具有默认值。
已定义DSResponse POJO以使json响应与链接上描述的同步-http://www.smartclient.com/smartgwt/javadoc/com/smartgwt/client/data/RestDataSource.html
Web.xml:调度程序Servlet条目
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
spring-servlet.xml –(基于注释,因此没有控制器bean仅映射到JacksonJsonView)
<context:component-scan base-package="com.khush.util.server.rest" />
<context:component-scan base-package="com.khush.util.server.spring" />
<context:annotation-config/>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<entry key="html" value="text/html"/>
<entry key="json" value="application/json"/>
</map>
</property>
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</list>
</property>
<property name="defaultViews">
<list>
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
<property name="prefixJson" value="true"/>
</bean>
</list>
</property>
</bean>
服务器端的Spring控制器–
@Controller
@RequestMapping("/service*")
public class RPCListService {
@RequestMapping(value="/fetch", method=RequestMethod.POST)
public @ResponseBody DSResponse fetch(@RequestBody String json){
DSResponse dsResponse = new DSResponse();
List<Account> accounts = new ArrayList<Account>();
Account account;
for(int i = 0 ; i < 500 ; i++){
account = new Account();
account.setAccountId(i * 100L);
account.setAccountName("i " + "accountName");
account.setAccountNumber("i " + "accountNumber");
accounts.add(account);
}
dsResponse.setDate(accounts.toArray());
return response;
}
@RequestMapping(value="/get", method=RequestMethod.GET)
@ResponseBody public Object get(@RequestBody String json){
DSResponse dsResponse = new DSResponse();
List<Account> accounts = new ArrayList<Account>();
Account account;
for(int i = 0 ; i < 500 ; i++){
account = new Account();
account.setAccountId(i * 100L);
account.setAccountName("i " + "accountName");
account.setAccountNumber("i " + "accountNumber");
accounts.add(account);
}
dsResponse.setDate(accounts.toArray());
return dsResponse;
}
}
客户端的RestDataSource定义–
public class AccountDataSource extends SpringJSONDataSource {
public AccountDataSource(List<DataSourceField> fields) {
super(fields);
setPrettyPrintJSON(true);
}
@Override
protected OperationBinding getFetch() {
OperationBinding fetch = new OperationBinding();
fetch.setOperationType(DSOperationType.FETCH);
fetch.setDataProtocol(DSProtocol.POSTMESSAGE);
DSRequest fetchProps = new DSRequest();
fetchProps.setHttpMethod(httpMethod);
fetch.setRequestProperties(fetchProps);
return fetch;
}
@Override
protected OperationBinding getRemove() {
// TODO Auto-generated method stub
return null;
}
@Override
protected OperationBinding getAdd() {
// TODO Auto-generated method stub
return null;
}
@Override
protected OperationBinding getUpdate() {
// TODO Auto-generated method stub
return null;
}
@Override
public String getUpdateDataURL() {
// TODO Auto-generated method stub
return null;
}
@Override
public String getRemoveDataURL() {
// TODO Auto-generated method stub
return null;
}
@Override
public String getAddDataURL() {
// TODO Auto-generated method stub
return null;
}
@Override
public String getFetchDataURL() {
return "/rest/service/fetch";
}
}
public abstract class SpringJSONDataSource extends RestDataSource {
protected final String httpMethod;
public SpringJSONDataSource(List<DataSourceField> fields){
this(fields, "POST");
}
public SpringJSONDataSource(List<DataSourceField> fields, String httpMethod) {
this.httpMethod = httpMethod;
setDataFormat(DSDataFormat.JSON);
addDataSourceFields(fields);
setOperationBindings(getFetch());
addURLs();
}
private void addURLs() {
if(getUpdateDataURL() != null)
setUpdateDataURL(getUpdateDataURL());
if(getRemoveDataURL() != null)
setRemoveDataURL(getRemoveDataURL());
if(getAddDataURL() != null)
setAddDataURL(getAddDataURL());
if(getFetchDataURL() != null)
setFetchDataURL(getFetchDataURL());
}
private void addDataSourceFields(List<DataSourceField> fields) {
for (DataSourceField dataSourceField : fields) {
addField(dataSourceField);
}
}
protected abstract OperationBinding getFetch();
protected abstract OperationBinding getRemove();
protected abstract OperationBinding getAdd();
protected abstract OperationBinding getUpdate();
public abstract String getUpdateDataURL();
public abstract String getRemoveDataURL();
public abstract String getAddDataURL();
public abstract String getFetchDataURL();
}
帐户和DSResponse对象–
public class Account implements Serializable {
private static final long serialVersionUID = 1L;
private Long accountId;
private String accountName;
private String accountNumber;
public Account(){
}
public Account(Long accountId){
setAccountId(accountId);
}
public Long getAccountId() {
return accountId;
}
public void setAccountId(Long accountId) {
this.accountId = accountId;
}
public String getAccountName() {
return accountName;
}
public void setAccountName(String accountName) {
this.accountName = accountName;
}
public String getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(String accountNumber) {
this.accountNumber = accountNumber;
}
@Override
public String toString(){
StringBuilder sb = new StringBuilder();
sb.append("Account Id: ").append(getAccountId()).append(", ");
sb.append("Account Name: ").append(getAccountName()).append(", ");
sb.append("Account Number: ").append(getAccountNumber()).append(", ");
return sb.toString();
}
@Override
public int hashCode(){
final int prime = 31;
int result = 1;
result = prime * result + ((accountName == null) ? 0 : accountName.hashCode());
return result;
}
@Override
public boolean equals(Object obj){
if(this == obj)
return true;
if(obj == null)
return false;
if(!(obj instanceof Account))
return false;
Account other = (Account)obj;
if(accountId == null){
if(other.accountId != null)
return false;
} else if(! accountId.equals(other.accountId))
return false;
if(accountName == null){
if(other.getAccountName() != null)
return false;
}else if(! accountName.equals(other.accountName))
return false;
if(accountNumber == null){
if(other.accountNumber != null)
return false;
}else if(! accountNumber.equals(other.accountNumber))
return false;
return true;
}
}
public class DSResponse implements Serializable{
private static final long serialVersionUID = -4937262101507779477L;
public static int STATUS_FAILURE = -1;
public static int STATUS_LOGIN_INCORRECT = -5;
public static int STATUS_LOGIN_REQUIRED = -7;
public static int STATUS_LOGIN_SUCCESS = -8;
public static int STATUS_MAX_LOGIN_ATTEMPTS_EXCEEDED = -6;
public static int STATUS_SERVER_TIMEOUT = -100;
public static int STATUS_SUCCESS = 0;
public static int STATUS_TRANSPORT_ERROR = -90;
public static int STATUS_VALIDATION_ERROR = -4;
private int status;
private Integer startRow;
private Integer endRow;
private Integer totalRows;
private Object[] data;
private Map errors;
public int getStatus()
{
return status;
}
public void setStatus(int status)
{
this.status = status;
}
public Integer getStartRow()
{
return startRow;
}
public void setStartRow(Integer startRow)
{
this.startRow = startRow;
}
public Integer getEndRow()
{
return endRow;
}
public void setEndRow(Integer endRow)
{
this.endRow = endRow;
}
public Integer getTotalRows()
{
return totalRows;
}
public void setTotalRows(Integer totalRows)
{
this.totalRows = totalRows;
}
public Object[] getData()
{
return data;
}
public void setData(Object[] data)
{
this.data = data;
}
public Map getErrors()
{
return errors;
}
public void setErrors(Map errors)
{
this.errors = errors;
}
public DSResponse()
{
status = STATUS_SUCCESS;
errors = new HashMap();
startRow = 0;
endRow = 0;
totalRows = -2;
}
public DSResponse(int status)
{
this.status = status;
errors = new HashMap();
startRow = 0;
endRow = 0;
totalRows = -1;
}
}
完整的堆栈跟踪:
09:24:58.896 [ERROR] [utilapp] 09:24:58.894:TMR0:WARN:RestDataSource:isc_AccountDataSource_0:RestDataSouce transformResponse():JSON响应文本似乎不是标准响应格式。
com.smartgwt.client.core.JsObject $ SGWT_WARN:09:24:58.894:TMR0:WARN:RestDataSource:isc_AccountDataSource_0:RestDataSouce transformResponse():JSON响应文本似乎不是标准响应格式。
在sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)处
在sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
在sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
在java.lang.reflect.Constructor.newInstance(Constructor.java:513)
在com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:105)
在com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
在com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
com.google.gwt.dev.shell.BrowserChannelServer.reactToMessagesWhileWaitingForReturn(BrowserChannelServer.java:338)
在com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:219)
在com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136)
在com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:571)
在com.google.gwt.dev.shell.ModuleSpace.invokeNativeVoid(ModuleSpace.java:299)
在com.google.gwt.dev.shell.JavaScriptHost.invokeNativeVoid(JavaScriptHost.java:107)
在com.smartgwt.client.data.DataSource.transformResponse(DataSource.java)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
在java.lang.reflect.Method.invoke(Method.java:597)
在com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
在com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
在com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
com.google.gwt.dev.shell.BrowserChannelServer.reactToMessagesWhileWaitingForReturn(BrowserChannelServer.java:338)
在com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:219)
在com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136)
在com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:571)
在com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:279)
在com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)
在com.google.gwt.core.client.impl.Impl.apply(Impl.java)
在com.google.gwt.core.client.impl.Impl.entry0(Impl.java:242)
在sun.reflect.GeneratedMethodAccessor50.invoke(未知来源)
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
在java.lang.reflect.Method.invoke(Method.java:597)
在com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
在com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
在com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
在com.google.gwt.dev.shell.BrowserChannelServer.reactToMessages(BrowserChannelServer.java:293)
在com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:547)
在com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:364)
在java.lang.Thread.run(Thread.java:619)
谢谢!
最佳答案
好吧,根据您的评论,您的回复中没有数据。您的回复与here指定的格式完全匹配吗?
鉴于您的错误消息是,我建议它可能不匹配
JSON响应文本似乎不是标准响应格式
接下来做什么?也许尝试将一些非常简单的对象转换为JSON,以查看问题是否出在您的Account对象上?尝试在响应中仅使用一个Account对象吗?或发布您的帐户对象代码?
关于java - Spring + GWT + jackson + JSON响应文本似乎不是标准响应格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16153956/