0

gwt 2.5 与 Jersey 1.17 和 RestyGWT 1.3

当我从客户端调用它时出现错误:Resposne 不是有效的 JASON 文档

它适用于整数,但为什么不适用于字符串???

我的资源:

@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() {

        }
    }
}
4

1 回答 1

0

我用 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());
        }
    }
}
于 2013-07-11T14:28:55.017 回答