java - How do I validate incoming JSON data inside a REST service? -
a rest service needs validate incoming json data against json schema. json schemas public accessible , can retrieved via http requests.
i'm using jackson-framwork marshaling , unmarshaling between java , json. far couldn't find possibility validate data against schema using jackson.
i tried jsontools framework comes such validation functionality. unfortunately wasn't possible me validation work. why jsontool schema validation isn't working?
how can such validation?
i searched best practice enforce validation incoming json data restful service. suggestion use messagebodyreader
performs validation inside readfrom
method. below there message-body-reader example non-generic sake of simplicity.
i interesed in finding best framework doing json data validation. because use jackson framework (version 1.8.5) marshaling , unmarshaling between json , java, have been nice if framework provide json data validation functionality. unfortunately couldn't find possibility jackson. got working json-schema-validator available @ https://github.com. version use 2.1.7
import java.io.bufferedreader; import java.io.bytearrayinputstream; import java.io.fileinputstream; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.lang.annotation.annotation; import java.lang.reflect.type; import javax.servlet.servletcontext; import javax.ws.rs.consumes; import javax.ws.rs.webapplicationexception; import javax.ws.rs.core.context; import javax.ws.rs.core.mediatype; import javax.ws.rs.core.multivaluedmap; import javax.ws.rs.ext.messagebodyreader; import javax.ws.rs.ext.provider; import org.codehaus.jackson.map.objectmapper; import at.fhj.ase.dao.data.address; import at.fhj.ase.xmlvalidation.msbreader.messagebodyreadervalidationexception; import com.fasterxml.jackson.databind.jsonnode; import com.github.fge.jackson.jsonloader; import com.github.fge.jsonschema.exceptions.processingexception; import com.github.fge.jsonschema.main.jsonschemafactory; import com.github.fge.jsonschema.main.jsonvalidator; import com.github.fge.jsonschema.report.processingreport; @provider @consumes(mediatype.application_json) public class addressjsonvalidationreader implements messagebodyreader<address> { private final string jsonschemafileasstring; public addressjsonvalidationreader(@context servletcontext servletcontext) { this.jsonschemafileasstring = servletcontext .getrealpath("/json/address.json"); } @override public boolean isreadable(class<?> type, type generictype, annotation[] annotations, mediatype mediatype) { if (type == address.class) { return true; } return false; } @override public address readfrom(class<address> type, type generictype, annotation[] annotations, mediatype mediatype, multivaluedmap<string, string> httpheaders, inputstream entitystream) throws ioexception, webapplicationexception { final string jsondata = getstringfrominputstream(entitystream); system.out.println(jsondata); inputstream isschema = new fileinputstream(jsonschemafileasstring); string jsonschema = getstringfrominputstream(isschema); /* * perform json data validation against schema */ validatejsondata(jsonschema, jsondata); /* * convert stream data entity */ objectmapper m = new objectmapper(); address addr = m.readvalue(stringtostream(jsondata), address.class); return addr; } /** * validate given json data against given json schema * * @param jsonschema * string * @param jsondata * string * @throws messagebodyreadervalidationexception * in case of error during validation process */ private void validatejsondata(final string jsonschema, final string jsondata) throws messagebodyreadervalidationexception { try { final jsonnode d = jsonloader.fromstring(jsondata); final jsonnode s = jsonloader.fromstring(jsonschema); final jsonschemafactory factory = jsonschemafactory.bydefault(); jsonvalidator v = factory.getvalidator(); processingreport report = v.validate(s, d); system.out.println(report); if (!report.tostring().contains("success")) { throw new messagebodyreadervalidationexception( report.tostring()); } } catch (ioexception e) { throw new messagebodyreadervalidationexception( "failed validate json data", e); } catch (processingexception e) { throw new messagebodyreadervalidationexception( "failed validate json data", e); } } /** * taken <a href= * "http://www.mkyong.com/java/how-to-convert-inputstream-to-string-in-java/" * >www.mkyong.com</a> * * @param * {@link inputstream} * @return stream content string */ private string getstringfrominputstream(inputstream is) { bufferedreader br = null; stringbuilder sb = new stringbuilder(); string line; try { br = new bufferedreader(new inputstreamreader(is)); while ((line = br.readline()) != null) { sb.append(line); } } catch (ioexception e) { e.printstacktrace(); } { if (br != null) { try { br.close(); } catch (ioexception e) { e.printstacktrace(); } } } return sb.tostring(); } private inputstream stringtostream(final string str) throws unsupportedencodingexception { return new bytearrayinputstream(str.getbytes("utf-8")); } }
Comments
Post a Comment