【问题标题】:RestyGWT and jerseyResponse is not Valid JSON documentRestyGWT 和 jerseyResponse 不是有效的 JSON 文档
【发布时间】:2013-07-08 15:52:18
【问题描述】:

gwt 2.5 与 Jersey 1.17 和 RestyGWT 1.3

当我打电话时

我收到以下错误:

无法解析响应:org.fusesource.restygwt.client.ResponseFormatException:响应不是有效的 JSON 文档

我的资源类:

@Path("/person")
public class PersonResource {

    private static int counter = 1;

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    @Path("/hello")
    public String sayHello(){
        return "Hallo, Seryoga";
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Person getPersons() {

        List<Person> persons = new ArrayList<Person>();
        for (int i = 0; i < 5; i++) {
            persons.add(new Person(counter, "name-" + counter, new Date()));
            counter++;
        }

        return persons.get(0);
    }
}

客户端界面

public interface PersonResourceAsync extends RestService {

    @GET
    void getPersons(MethodCallback<Person> callback);

    /**
     * Utility class to get the instance of the Rest Service
     */
    public static final class Util {

        private static PersonResourceAsync instance;

        public static final PersonResourceAsync get() {
            if (instance == null) {
                instance = GWT.create(PersonResourceAsync.class);
                ((RestServiceProxy) instance).setResource(new Resource(
                        GWT.getHostPageBaseURL()+"rest/person"));
            }
            return instance;
        }

        private Util() {

        }
    }
}

person 类有一些用于 id 名称的 getter/setter 等可序列化和 @XmlRootElement

我的 web.xml 包含:

<servlet>
        <servlet-name>RestServlet</servlet-name>
        <display-name>Rest Servlet</display-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>digitronic.ems.server.filebrowser</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
            <param-value>true</param-value>

        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

在 projekt.gwt.xml 中:

<!-- RestyGWT -->
<inherits name='org.fusesource.restygwt.RestyGWT' />

当我通过 http://localhost:8080/Projekt/rest/person 我得到 json 语法中的值 但是通过restygwt我调用了onError,我不知道为什么。

有人可以帮我吗?

【问题讨论】:

  • 您确定定位到正确的网址吗?你用`F12`检查了吗?
  • 是的,因为当我从客户端调用时我可以在服务器上进行调试,我得到一个 jason 对象但无法解析它

标签: gwt jersey jax-rs tomcat6


【解决方案1】:

您需要在入口点类中为默认编码/解码的日期对象设置以下代码。

static {
        Defaults.setDateFormat(null);
    }

【讨论】:

    【解决方案2】:

    我有类似的设置,而我这边的问题是 Date 对象没有正确传输。我为 Date 对象添加了一个适配器。 只需将两个文件放在您的 Person 类所在的同一个包中:

    public class MyJaxbDateAdapter extends XmlAdapter<String,Date>{
    //standard jaxb/resty ISO8601 date format
    public static final String DATEFORMAT="yyyy-MM-dd'T'HH:mm:ss.SSSZ";
    
    public MyJaxbDateAdapter(){}
    
    public Date unmarshal(String s)    throws Exception    {
        if(s==null || s.length()==0){
            return null;
        }
        return new SimpleDateFormat(DATEFORMAT, Locale.GERMANY).parse(s);
    }
    
    public String marshal(Date d)     throws Exception     {
        if(d==null) {
            return "";
        }
        return new SimpleDateFormat(DATEFORMAT, Locale.GERMANY).format(d);
    }
    }
    

    还有一个文件 package-info.java:

    @javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters
        ({
    @javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter(value=MyJaxbDateAdapter.class,type= java.util.Date.class)
    
        })
    package de.company.app;
    

    如果这没有帮助,请为 Person 类添加您的代码。

    【讨论】:

      猜你喜欢
      • 2018-05-07
      • 1970-01-01
      • 2013-08-07
      • 1970-01-01
      • 2014-03-02
      • 1970-01-01
      • 2013-08-26
      • 2011-12-15
      • 2013-12-28
      相关资源
      最近更新 更多