org.fusesource.restygwt.client.ResponseFormatException: Response was NOT a valid JSON document

395 views
Skip to first unread message

Salman P. Dar

unread,
Jan 29, 2018, 5:01:15 PM1/29/18
to RestyGWT
Hi guys,

I'm getting a Response was NOT a valid JSON document which doesn't make sense.
I have a GWT app calling a Spring MVC with Spring Security as backend - all REST.

In my browser I can see the following being returned by the Spring-application:

{"id":1,"username":"admin","enabled":true,"accountExpired":false,"accountLocked":false,"passwordExpired":false,"firstName":"Admin","lastName":"Administrator","dateOfBirth":"01.01.1970","email":"some...@gmail.com","telephone":"","mobile":"","title":"Developer","initials":"ADM","address":"","postalCode":"","country":"","description":"","created":946681200007,"modified":null,"lastLogin":1517262304842}

Upon login, above is sent by the code below:
UserDetails userDetails = (UserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
User user = userService.findByUsername(userDetails.getUsername());
user.setLastLogin(Calendar.getInstance().getTime());
userService.saveUser(user);
log.debug("Fetched user: " + user.getUsername() + " (" + user.getFirstName() + " " + user.getLastName() + ") - sending it through the response writer...");
SecurityUtils.sendResponse(response, HttpServletResponse.SC_OK, user);

The SecurityUtils.sendResponse is as follows:
    public static void sendResponse(HttpServletResponse response, int status, Object object) throws IOException {
        response.setContentType("application/json;charset=UTF-8");
        PrintWriter writer = response.getWriter();
        log.debug("mapper.writeValueAsString(object): " + mapper.writeValueAsString(object));
        writer.write(mapper.writeValueAsString(object));
        response.setStatus(status);
        writer.flush();
        writer.close();
    }

Above mapper uses:
import com.fasterxml.jackson.databind.ObjectMapper;


My call from GWT to the login rest method is (expecting a User-object in return):
LoginRest.Util.get().login(username.value, password.value, rememberMe.value, new MethodCallback<User>() {
@Override
public void onSuccess(Method method, User user) {
logger.log(Level.SEVERE, "SUCCESS");
logger.log(Level.SEVERE, "Cookie JSESSION ID: " + Cookies.getCookie("JSESSIONID"));
loginMessage.innerHTML = user.getFirstName() + " " + user.getLastName() + " " + user.getEmail();
}
@Override
public void onFailure(Method method, Throwable exception) {
logger.log(Level.SEVERE, "FAILURE");
logger.log(Level.SEVERE, exception.toString());
for (StackTraceElement element : exception.getStackTrace()) {
    logger.log(Level.SEVERE, element.toString());
}
if(exception instanceof FailedResponseException) {
FailedResponseException fre1 = (FailedResponseException) exception;
    JSONObject jObject = JSONParser.parseStrict(fre1.getResponse().getText()).isObject();
    logger.log(Level.SEVERE, "Login failed! 'Access': " + jObject.get("authentication").isString().stringValue());
}
}
});

The exception.toString() is giving below in browser console:
Mon Jan 29 22:45:04 GMT+100 2018 NameOfYourLogger
SEVERE: org.fusesource.restygwt.client.ResponseFormatException: Response was NOT a valid JSON document croozegwt-0.js:1:6519
Kj croozegwt-0.js:1:6519 qr
pr
wr
Ik
Kk croozegwt-0.js:1:8318 el
rc
Lc croozegwt-0.js:1:3285 zk/c.onreadystatechange<
Gb
Jb
Ib/<

Doesn't make sense. I found this:
But it doesn't make sense - it's quite old and besides, in the documentation:
It is clearly stated, that:
The JSON encoding style is compatible with the default Jackson Data Binding. For example, PizzaOrder’s JSON representation would look like:
{
    "phone_number":null,
    "delivery":true,
    "delivery_address":[
        "3434 Pinerun Ave.",
        "Wesley Chapel, FL 33734"
    ],
    "pizzas":[
        {"quantity":1,"size":16,"crust":"thin","toppings":["ham","pineapple"]},
        {"quantity":1,"size":16,"crust":"thin","toppings":["extra cheese"]}
    ]
}

The problem shouldn't be non-quoted values, right?

Btw, the User-object in GWT is (the same in the Spring app but with javax.persistent-annotations etc.):
public class User {

private Long id;
private String username;
private boolean enabled;
private boolean accountExpired;
private boolean accountLocked;
private boolean passwordExpired;
private String firstName;
private String lastName;
private String dateOfBirth;
private String email;
private String telephone;
private String mobile;
private String title;
private String initials;
private String address;
private String postalCode;
private String country;
private String description;
private Date created;
private Date modified;
private Date lastLogin;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public boolean isAccountExpired() {
return accountExpired;
}
public void setAccountExpired(boolean accountExpired) {
this.accountExpired = accountExpired;
}
public boolean isAccountLocked() {
return accountLocked;
}
public void setAccountLocked(boolean accountLocked) {
this.accountLocked = accountLocked;
}
public boolean isPasswordExpired() {
return passwordExpired;
}
public void setPasswordExpired(boolean passwordExpired) {
this.passwordExpired = passwordExpired;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(String dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getInitials() {
return initials;
}
public void setInitials(String initials) {
this.initials = initials;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPostalCode() {
return postalCode;
}
public void setPostalCode(String postalCode) {
this.postalCode = postalCode;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}
public Date getModified() {
return modified;
}
public void setModified(Date modified) {
this.modified = modified;
}
public Date getLastLogin() {
return lastLogin;
}
public void setLastLogin(Date lastLogin) {
this.lastLogin = lastLogin;
}

}



Salman P. Dar

unread,
Jan 29, 2018, 5:05:48 PM1/29/18
to RestyGWT
My pom.xml in the GWT-project:
<?xml version="1.0" encoding="UTF-8"?>

  <!-- POM file generated with GWT webAppCreator -->
  <modelVersion>4.0.0</modelVersion>
  <groupId>dk.crooze.gwt</groupId>
  <artifactId>CroozeGwt</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>dk.crooze.gwt.CroozeGwt</name>

  <properties>

    <!-- Setting maven.compiler.source to something different to 1.8
         needs that you configure the sourceLevel in gwt-maven-plugin since
         GWT compiler 2.8 requires 1.8 (see gwt-maven-plugin block below) -->
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>

    <!-- Don't let your Mac use a crazy non-standard encoding -->
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  </properties>

  <dependencyManagement>
    <dependencies>
      <!-- ensure all GWT deps use the same version (unless overridden) -->
      <dependency>
        <groupId>com.google.gwt</groupId>
        <artifactId>gwt</artifactId>
        <version>2.8.2</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <dependencies>
    <dependency>
      <groupId>com.google.gwt</groupId>
      <artifactId>gwt-servlet</artifactId>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>com.google.gwt</groupId>
      <artifactId>gwt-user</artifactId>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>com.google.gwt</groupId>
      <artifactId>gwt-dev</artifactId>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20171018</version>
</dependency>
    <!-- For JAX-RS annotations, such as @GET, @POST, @Path, @PathParam, @QueryParam, etc -->
    <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>javax.ws.rs-api</artifactId>
        <version>2.1</version>
    </dependency>
    
    <!-- For client-side code to query a REST/JSON server API. This uses methods and classes annotated with JAX-RS annotations, doing serialization/deserialization with Jackson. -->
    <dependency>
        <groupId>org.fusesource.restygwt</groupId>
        <artifactId>restygwt</artifactId>
        <version>2.2.2</version>
    </dependency>
    
    <!-- For JSON serialization/deserialization based on the JAX-RS annotations. Used by RestyGWT on the client side. Also adds some annotations such as @JsonInclude and @JsonIgnore, -->
    <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-json-provider</artifactId>
        <version>2.9.3</version>
    </dependency>
    
    <!-- To serve REST/JSON queries of REST resources.
     A <servlet> tag in the web.xml file indicates that Jersey should use certain classes as REST resources/servelets.
     These REST resources/servlets use Java methods and classes annotated with JAX-RS and Jackson annotations.
 
     jersey-container-servlet needs the Java Servlet API version 3, supported by the AppEngine Java 8 runtime (currently beta).
     Alternatively, jersey-container-servlet-core needs the Java Servlet API version 2, supported by the AppEngine Java 7 runtime. -->
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>2.26</version>
    </dependency>
    
    <!-- To let Jersey use Jackson. -->
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.26</version>
    </dependency>
    
    <!-- Workaround this error: java.lang.IllegalStateException: InjectionManagerFactory not found. See https://stackoverflow.com/a/44546979/1123654 -->
    <dependency>
        <groupId>org.glassfish.jersey.inject</groupId>
        <artifactId>jersey-hk2</artifactId>
        <version>2.26</version>
    </dependency>
<dependency>
    <groupId>org.jboss.gwt.elemento</groupId>
    <artifactId>elemento-core</artifactId>
    <version>0.7.0</version>
</dependency>
<dependency>
    <groupId>org.jboss.gwt.elemento</groupId>
    <artifactId>elemento-template</artifactId>
    <version>0.6.2</version>
</dependency>
<dependency>
    <groupId>com.googlecode.gwt-crypto</groupId>
    <artifactId>gwt-crypto</artifactId>
    <version>2.3.0</version>
</dependency>

  </dependencies>

  <build>
    <!-- Output classes directly into the webapp, so that IDEs and "mvn process-classes" update them in DevMode -->
    <outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>

    <plugins>

      <!-- GWT Maven Plugin-->
      <plugin>
        <groupId>net.ltgt.gwt.maven</groupId>
        <artifactId>gwt-maven-plugin</artifactId>
        <version>1.0-rc-9</version>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>
              <goal>test</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <moduleName>dk.crooze.gwt.CroozeGwt</moduleName>
          <moduleShortName>CroozeGwt</moduleShortName>
          <failOnError>true</failOnError>
          <!-- GWT compiler 2.8 requires 1.8, hence define sourceLevel here if you use
               a different source language for java compilation -->
          <sourceLevel>1.8</sourceLevel>
          <!-- Compiler configuration -->
          <compilerArgs>
            <!-- Ask GWT to create the Story of Your Compile (SOYC) (gwt:compile) -->
            <arg>-compileReport</arg>
            <arg>-XcompilerMetrics</arg>
          </compilerArgs>
          <!-- DevMode configuration -->
          <warDir>${project.build.directory}/${project.build.finalName}</warDir>
          <classpathScope>compile+runtime</classpathScope>
          <!-- URL(s) that should be opened by DevMode (gwt:devmode). -->
          <startupUrls>
            <startupUrl>CroozeGwt.html</startupUrl>
          </startupUrls>
        </configuration>
      </plugin>

      <!-- Skip normal test execution, we use gwt:test instead -->
      <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.17</version>
        <configuration>
          <skip>true</skip>
        </configuration>
      </plugin>

    </plugins>
  </build>
</project>




On Monday, 29 January 2018 23:01:15 UTC+1, Salman P. Dar wrote:
Hi guys,

I'm getting a Response was NOT a valid JSON document which doesn't make sense.
I have a GWT app calling a Spring MVC with Spring Security as backend - all REST.

In my browser I can see the following being returned by the Spring-application:

{"id":1,"username":"admin","enabled":true,"accountExpired":false,"accountLocked":false,"passwordExpired":false,"firstName":"Admin","lastName":"Administrator","dateOfBirth":"01.01.1970","email":"someemail...@gmail.com","telephone":"","mobile":"","title":"Developer","initials":"ADM","address":"","postalCode":"","country":"","description":"","created":946681200007,"modified":null,"lastLogin":1517262304842}

Salman P. Dar

unread,
Jan 30, 2018, 2:58:44 PM1/30/18
to RestyGWT
SOLVED:

Added the following in the GWT-pom.xml:

<dependency>
<groupId>com.github.nmorel.gwtjackson</groupId>
<artifactId>gwt-jackson</artifactId>
<version>0.15.0</version>
</dependency>


And to the project.gwt.xml file:

  <inherits name="com.github.nmorel.gwtjackson.GwtJackson"/>
  <set-property name="restygwt.encodeDecode.useGwtJackson" value="true"/>


It's working now!


On Monday, 29 January 2018 23:01:15 UTC+1, Salman P. Dar wrote:
Hi guys,

I'm getting a Response was NOT a valid JSON document which doesn't make sense.
I have a GWT app calling a Spring MVC with Spring Security as backend - all REST.

In my browser I can see the following being returned by the Spring-application:

{"id":1,"username":"admin","enabled":true,"accountExpired":false,"accountLocked":false,"passwordExpired":false,"firstName":"Admin","lastName":"Administrator","dateOfBirth":"01.01.1970","email":"someemail...@gmail.com","telephone":"","mobile":"","title":"Developer","initials":"ADM","address":"","postalCode":"","country":"","description":"","created":946681200007,"modified":null,"lastLogin":1517262304842}

Hugo Haas

unread,
May 6, 2019, 5:36:40 PM5/6/19
to RestyGWT
Very good! I'm finding the solution for this problem and you help me a lot! 

Thanks!

Hugo Haas

unread,
May 7, 2019, 6:20:40 PM5/7/19
to RestyGWT
humm...  not yet...!!!

For some DTOs the solution with gwtjackson solve, but in another DTOs I'm still having the same problem: Response was NOT a valid JSON document

I read on gwtresty documentation that gwtjackson is yet an experimental library...

I'm using 2.2.0 restygwt on a 2.7.0 gwt

There is another solution?

Hugo Haas

unread,
May 8, 2019, 11:04:49 AM5/8/19
to RestyGWT
SOLVED: A lot of my system DTOs uses a Date atributes and for some reason (that I don't know) the expected date format was set to diferent of mileseconds unix format. I just add the bellow code to configure to decode uses a Unix Milesecond Format

CODE:   Defaults.setDateFormat(null);

I found this tip on  https://resty-gwt.github.io/documentation/restygwt-user-guide.html, in a DATE FORMAT topic.

I added the code on main entrypoint, just to declare on time on my application, than all calls to server will use this configuration to decode all JSON return.  :-)
Reply all
Reply to author
Forward
0 new messages