java - Jackson's Access.WRITE_ONLY during test null -
i'm playing around jackson's de/serialization features , encountered problem, don't know how solve.
during test @jsonproperty(access = jsonproperty.access.write_only)
annotation ignored , shows null
. e.g. postman works expected.
i using spring boot starter web starter , test starter dependency.
example code:
@springbootapplication public class demoapplication { public static void main(string[] args) { springapplication.run(demoapplication.class, args); } } @restcontroller class jacksonexamplerestcontroller { @postmapping("/api") public void getresti(@requestbody jacksonmodel jacksonmodel) { system.out.println(jacksonmodel.getid()); system.out.println(jacksonmodel.getpassword()); } } class jacksonmodel { private string id; @jsonproperty(access = jsonproperty.access.write_only) private string password; public string getid() { return id; } public void setid(string id) { this.id = id; } public string getpassword() { return password; } public void setpassword(string password) { this.password = password; } }
test:
@runwith(springrunner.class) @springboottest(classes = demoapplication.class) public class demoapplicationtests { private mockmvc mockmvc; @before public void setup() { jacksonexamplerestcontroller jacksonexamplerestcontroller = new jacksonexamplerestcontroller(); mockmvc = mockmvcbuilders.standalonesetup(jacksonexamplerestcontroller) .build(); } @test public void testjackson() throws exception { jacksonmodel jacksonmodel = new jacksonmodel(); jacksonmodel.setid("id"); jacksonmodel.setpassword("password"); mockmvc.perform(post("/api"). contenttype(application_json_utf8) .content(convertobjecttojsonbytes(jacksonmodel))); } public static byte[] convertobjecttojsonbytes(object object) throws ioexception { objectmapper mapper = new objectmapper(); mapper.setserializationinclusion(jsoninclude.include.non_null); return mapper.writevalueasbytes(object); } }
is default behaviour , have configure in test or else don't see right now?
Comments
Post a Comment