gwt 2.5,泽西岛1.17和RestyGWT 1.3
当我从客户端调用它时出现错误: Resposne不是有效的JASON文件
它适用于Integer,但为什么不使用Strings ???
我的资源:
@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() {
}
}
}
答案 0 :(得分:0)
我用MessageBodyWriter解决了它,它将String设置为引号
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());
}
}
}