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);
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(); }
import com.fasterxml.jackson.databind.ObjectMapper;
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()); } } });
Mon Jan 29 22:45:04 GMT+100 2018 NameOfYourLoggerSEVERE: org.fusesource.restygwt.client.ResponseFormatException: Response was NOT a valid JSON document croozegwt-0.js:1:6519Kj croozegwt-0.js:1:6519 qrprwrIkKk croozegwt-0.js:1:8318 elrcLc croozegwt-0.js:1:3285 zk/c.onreadystatechange<GbJbIb/<
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"]}
]
}
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; }
}
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<!-- 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. --> <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.containers/jersey-container-servlet --> <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>
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}
<dependency> <groupId>com.github.nmorel.gwtjackson</groupId> <artifactId>gwt-jackson</artifactId> <version>0.15.0</version> </dependency>
<inherits name="com.github.nmorel.gwtjackson.GwtJackson"/> <set-property name="restygwt.encodeDecode.useGwtJackson" value="true"/>
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}