返回值为字符串时的RestyGwt ResponseFormatException
gwt 2.5,球衣1.17和RestyGWT 1.3 当我从客户端调用它时,我得到一个错误: Resposne不是有效的JASON文档 它适用于整数,但为什么不适用于字符串 我的资源:返回值为字符串时的RestyGwt ResponseFormatException,gwt,jersey,jax-rs,Gwt,Jersey,Jax Rs,gwt 2.5,球衣1.17和RestyGWT 1.3 当我从客户端调用它时,我得到一个错误: Resposne不是有效的JASON文档 它适用于整数,但为什么不适用于字符串 我的资源: @Path("/files") public class FileResource { @GET @Produces(MediaType.APPLICATION_JSON) @Path("/backup") public String getBackup() {
@Path("/files")
public class FileResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/backup")
public String getBackup() {
return new String("asd");
}
和RestService接口:
public interface FileRestService extends RestService {
@GET
@Path("/backup")
void getBackup(MethodCallback<String> callback);
/**
* Utility class to get the instance of the Rest Service
*/
public static final class Util {
private static FileRestService instance;
public static final FileRestService get() {
if (instance == null) {
instance = GWT.create(FileRestService.class);
((RestServiceProxy) instance).setResource(new Resource(GWT
.getHostPageBaseURL() + "rest/files"));
}
return instance;
}
private Util() {
}
}
}
公共接口FileRestService扩展了RestService{
@得到
@路径(“/backup”)
void getBackup(MethodCallback);
/**
*获取Rest服务实例的实用程序类
*/
公共静态最终类Util{
私有静态FileRestService实例;
公共静态最终FileRestService get(){
if(实例==null){
instance=GWT.create(FileRestService.class);
((RestServiceProxy)实例).setResource(新资源(GWT
.getHostPageBaseURL()+“rest/files”);
}
返回实例;
}
私有Util(){
}
}
}
我用MessageBodyWriter解决了这个问题,它将字符串设置为引号
package digitronic.ems.server.filebrowser;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;
import org.json.JSONObject;
@Provider
@Produces(MediaType.APPLICATION_JSON)
public class StringProvider implements MessageBodyWriter<String> {
@Override
public boolean isWriteable(Class<?> type, Type genericType,
Annotation[] annotations, MediaType mediaType) {
return type == String.class;
}
@Override
public long getSize(String t, Class<?> type, Type genericType,
Annotation[] annotations, MediaType mediaType) {
return t.length();
}
@Override
public void writeTo(String t, Class<?> type, Type genericType,
Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders,
OutputStream entityStream) throws IOException,
WebApplicationException {
if (mediaType.toString().equals(MediaType.APPLICATION_JSON)) {
String wr = JSONObject.quote(t);
entityStream.write(wr.getBytes());
}
}
}
包digitronic.ems.server.filebrowser;
导入java.io.IOException;
导入java.io.OutputStream;
导入java.lang.annotation.annotation;
导入java.lang.reflect.Type;
导入javax.ws.rs.products;
导入javax.ws.rs.WebApplicationException;
导入javax.ws.rs.core.MediaType;
导入javax.ws.rs.core.MultivaluedMap;
导入javax.ws.rs.ext.MessageBodyWriter;
导入javax.ws.rs.ext.Provider;
导入org.json.JSONObject;
@提供者
@产生(MediaType.APPLICATION_JSON)
公共类StringProvider实现MessageBodyWriter{
@凌驾
公共布尔值可写(类类型、类型genericType、,
注释[]注释,MediaType(MediaType){
返回类型==String.class;
}
@凌驾
公共长getSize(字符串t、类类型、类型genericType、,
注释[]注释,MediaType(MediaType){
返回t.length();
}
@凌驾
public void writeTo(字符串t、类类型、类型genericType、,
注释[]注释,MediaType MediaType,
多值MAP HttpHeader,
OutputStream entityStream)引发IOException,
WebApplicationException{
if(mediaType.toString().equals(mediaType.APPLICATION_JSON)){
字符串wr=JSONObject.quote(t);
write(wr.getBytes());
}
}
}
注意,根据规范,JSON需要一个顶级对象或数组(JSON.parse()
在JavaScript中有一个宽松的解析器,它还接受字符串、数字、布尔值和null
)。hm ok它与返回的新字符串(“asd”)一起工作;我怀疑Jersey的JSON支持将字符串
返回值视为包含JSON。请注意,'asd'
也不是有效的JSON:字符串文本必须在JSON中用双引号引起来,而不是用撇号引起来。看见