How to generate JSON response from JSF?
JSF is a MVC framework generating HTML, not some kind of a REST web service framework. You're essentially abusing JSF as a web service. Your concrete problem is simply caused by placing <html>
tags and so on in the view file yourself.
If you really insist, then you can always achieve this by using <ui:composition>
instead of <html>
. You also need to make sure that the right content type of application/json
is been used, this defaults in JSF namely to text/html
.
<ui:composition
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets">
<f:event type="preRenderView" listener="#{bean.renderJson}" />
</ui:composition>
with
public void renderJson() throws IOException {
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
externalContext.setResponseContentType("application/json");
externalContext.setResponseCharacterEncoding("UTF-8");
externalContext.getResponseOutputWriter().write(someJsonString);
facesContext.responseComplete();
}
But I strongly recommend to look at JAX-RS or JAX-WS instead of abusing JSF as a JSON web service. Use the right tool for the job.
See also:
- How to implement JAX-RS RESTful service in JSF framework
- Servlet vs RESTful
- What is the need of JSF, when UI can be achieved from CSS, HTML, JavaScript, jQuery?
I'm even using contentType="text/xhtml" with JSF 2.2 and it works great. No needs in renderJson() from the BalusC's answer above
<f:view encoding="UTF-8" contentType="text/html"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:outputText value="#{stationView.getClosestStationen(param.longitude, param.latitude)}" escape="false"/>
</f:view>
Ajax call:
$.ajax({
url: requestContextPath + '/rest/stationen.xhtml',
type: "GET",
data: {
"longitude": x,
"latitude": y
},
dataType: "json",
success: function (data) {
$.each(data, function (i, station) {
...
});
},
error: function () {
...
}
})