spring - The specified HTTP method is not allowed for the requested resource -
i using spring 4.1 , rest controller signature looks like:
@restcontroller @requestmapping("/api/device") public class apirestcontroller { public responseentity<response> singleresponse() { system.out.println("sdr get"); singledataresponse res = new singledataresponse(); res.setcmd("8028"); res.setdata("xyz"); res.setrfu("rfu"); res.setsid("99"); return new responseentity<response>(res, httpstatus.ok); } @requestmapping(value = "/single-res-post", method = requestmethod.post) @responsestatus(httpstatus.created) public responseentity<response> singleresponsepost( @requestbody singledatarequest req, httpservletrequest request, httpservletresponse response) { system.out.println("sdr post"); singledataresponse res = new singledataresponse(); res.setcmd(req.getcmd()); res.setrfu(req.getrfu()); res.setsid(req.getsid()); res.setdata("0"); return new responseentity<response>(res, httpstatus.ok); } }
i have added below bean in webmvcconfig
@bean public requestmappinghandleradapter requestmappinghandleradapter() { requestmappinghandleradapter adapter = new requestmappinghandleradapter(); adapter.setcachesecondsforsessionattributehandlers(0); final mappingjackson2httpmessageconverter mappingjacksonhttpmessageconverter = new mappingjackson2httpmessageconverter(); list<httpmessageconverter<?>> httpmessageconverter = new arraylist<httpmessageconverter<?>>(); httpmessageconverter.add(mappingjacksonhttpmessageconverter); string[] supportedhttpmethods = { "post", "get", "head" }; adapter.setmessageconverters(httpmessageconverter); adapter.setsupportedmethods(supportedhttpmethods); return adapter; }
i using httpclient , post access above rest api
i able acess request while sending post request getting response
<!doctype html> <html><head><title>apache tomcat/8.0.12 - error report</title></head> <body> <h1>http status 405 - request method 'post' not supported</h1> <p><b>message</b> <u>request method 'post' not supported</u></p> <p><b>description</b> <u>the specified http method not allowed requested resource.</u></p> </body></html>
kindly suggest
the code used send post request
public static void verifyireowspost() throws exception { string wms = "http://localhost:8080/test-rest/api/device/single-res-post"; map<string, string> headers = new hashmap<string, string>(); string json = "{\"sid\":\"99\",\"rfu\":\"rfu\",\"cmd\":\"8028\",\"data\":\"xyz\"}"; headers.put(httpheaders.content_type, "application/json"); headers.put("username", "admin"); headers.put("password", "admin"); string response = httputils.sendpost(wms, headers, json); system.out.println("post response: " + response); }
Comments
Post a Comment