This Confluence has been LDAP enabled, if you are an ASF Committer, please use your LDAP Credentials to login. Any problems file an INFRA jira ticket please.

Child pages
  • How to write JSON response
Skip to end of metadata
Go to start of metadata

In your WebPage...

...

public class JsonWebPage extends WebPage {

	public JsonWebPage(PageParameters pageParameters) {

		getRequestCycle().scheduleRequestHandlerAfterCurrent(new IRequestHandler() {

			@Override
			public void detach(RequestCycle requestCycle) {
				// Nothing to do here.
			}

			@Override
			public void respond(RequestCycle requestCycle) {
				// Add JSON-encoded string to the response.
				Response response = requestCycle.getResponse();
                                response.setContentType("application/json");
                                response.write("{\"jsonKey\":\"jsonValue\"}");
			}
		});
	}
}
...

You can use TextRequestHandler to simplify the code above.

NOTE: You DO NOT need to manually write JSON response, there are libraries like http://json-lib.sourceforge.net/ available that will allow you to convert an object hierarchy to something more useful in terms of JSON rather easily. For details, check the documentation at: http://json-lib.sourceforge.net/usage.html.

Another library for JSON processing/generation is jackson.

NOTE Wicket >= 1.5.x: see https://github.com/wicketstuff/core/tree/master/jdk-1.6-parent/autocomplete-tagit-parent for an example

  • No labels