java - Getting a RESTful webservice with Spring to understand a JSON-string -
I am very new to spring and all things involved, but I am trying to get through this medium. / P>
To understand the JSON-formatted string, I am trying to create a service using Spring MVN and Gradle. It's all made in Java but I can not do it at all.
@RequestMapping (value = "/ toggle") public @ ResponseBody String sendCommand (@RequestParam (value = "IP") string IP) {// body } There is a method in my controller, but when I send the following JSON-formatted string <<> {"IP": "1982.19 "} using an @RequestBody for an example, with an own class for the intranet. And another where I get the HTTPTTP, to see if the string was received correctly. Upon receiving the string correctly, I can not get my code to "read" it. I can go to the local host: 8080 / toggle, and set variables by HTTP, say:
http: // localhost: 8080 / toggle? Ip = 192.168.1.9 works.
Where would you like to continue troubleshooting? thank you in advanced!
EDIT: Many thanks for the wow quick reply, I was expecting a day or two to answer, instead I got you help in minutes. I knew this site was good, but you are very good, thanks!
He said, I have created a new class called a, which is only a private string variable, in which the constructor, setter and guest are. Now I have
@RequestMapping (value = "/ toggle") public @ResponseBody send string command (@RequestBody ident ident) {// body} I'm still sending the same string 400.
And when it will work using a @ path varabil, then I should do this work, because now I am just experimenting with it. It will only be used later to send more than the IP only.
I am using the @ComponentScan @ enabled password in the main file, maybe doing something with things - it gets set up incorrectly?
You are getting a different thing (request parameters and Jason objects). To capture {"IP": "192.168.1.9"} to post, you have to do something like: @RequestMapping (value = "/ toggle") Public @ResponseBody Send String Commands (@RequestBody String Jason) {Last YourObject yourObject = mapJsonToObject (json); Last string IP = yourObject.getIp (); // body} You also need to create a square that corresponds to json and map Jason (using a library like Jackson) on that class.
If you want to discard the creation of an intermediate object (which I did not offer you because this code makes cleaner), you can simply remove the IP using standard JDK methods from JSB string.
Comments
Post a Comment