使用Spring获取RESTful Web服务以了解JSON字符串 [英] Getting a RESTful webservice with Spring to understand a JSON-string

查看:78
本文介绍了使用Spring获取RESTful Web服务以了解JSON字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对使用Spring以及涉及的所有事物都是新手,但我正在努力解决这一问题.

I'm very new to using Spring, and all the things involved, but I'm trying to get through this.

我正在尝试使用Spring MVN和Gradle提供服务,以了解JSON格式的字符串.这些都是用Java制作的. 但是我根本无法使其正常工作.

I'm trying to make a service, using the Spring MVN and Gradle, to understand a JSON-formatted String. It's all made in Java. But I can't get it to work as it should, at all.

@RequestMapping(value = "/toggle")
public @ResponseBody String sendCommand(@RequestParam(value="IP") String IP){
//body
}

是Controller中的一种方法,但是,当我发送以下JSON格式的字符串时

Is a method in my Controller, but, when I send the following JSON-formatted String

{"IP":"192.168.1.9"}

我仍然收到响应代码400.

I still get the Response Code 400.

我尝试了不同的变体,其中一个示例使用@RequestBody,并为indata提供了自己的类.还有一个我接收到HttpEntity的地方,以查看是否正确接收了String.正确接收到字符串后,我无法获取代码来读取"它.

I've tried different variations, one example using a @RequestBody instead, with an own class for the indata. And another where I recieve a HttpEntity, to watch whether the String was recieved correctly. While the String is recieved correctly, I can't get my code to "read" it.

我可以转到localhost:8080/toggle,并通过HTTP设置变量,例如:

I can go to localhost:8080/toggle, and through HTTP set the variables, say:

http://localhost:8080/toggle?IP=192.168.1.9

有效.

您建议在哪里继续进行故障排除? 预先感谢!

Where would you suggest to continue troubleshooting? Thanks in advance!

哇.非常感谢您的快速回复,我希望在一两天之内没有回复,而我却在几分钟之内得到了您的帮助.我知道这个网站很好,但是您太棒了,谢谢!

Wow. Thanks a lot for quick replies, I was expecting a day or two to go without replies, instead I got your help within minutes. I knew this site was good, but you are great, thanks!

也就是说,我创建了一个名为Ident的新类,该类只有一个私有String变量,其中包含构造函数,setter和getter. 现在我有

That said, I made a new class called Ident, which has only a private String variable, with constructor, setter and getter. Now I have

@RequestMapping(value = "/toggle")
public @ResponseBody String sendCommand(@RequestBody Ident ident) {
//body
}

虽然我仍然有400个发送相同的字符串.

I still get 400 sending the same String though.

虽然它可以使用@PathVariable起作用,但我想使它起作用,因为现在我仅对其进行试验.以后将用于发送更多的信息,而不只是IP.

And while it would work using a @PathVariable, I'd like getting this to work, because now I'm only experimenting with it. It will be used to send more than only the IP later on.

我正在使用 @ComponentScan @EnableAutoConfiguration 在主文件中,可能与事情有关-设置错误吗?

I'm using @ComponentScan @EnableAutoConfiguration in the main-file, might that have something to do with things - that it gets set up wrong?

推荐答案

您正在混合使用其他东西(请求参数和json对象).

You are mixing to different thing (request parameters and json objects).

要捕获已发布的{"IP":"192.168.1.9"},您需要执行以下操作:

In order to catch the POSTed {"IP":"192.168.1.9"} you need to do something like the following:

@RequestMapping(value = "/toggle")
public @ResponseBody String sendCommand(@RequestBody String json){
   final YourObject yourObject = mapJsonToObject(json);
   final String ip = yourObject.getIp();
//body
}

您还需要创建一个与json对应的类,并将该json映射到该类(使用诸如jackson之类的库).

You also need to create a class that corresponds to the json and map that json to the class (using a library like jackson).

如果您要放弃中间对象的创建(我不建议您这样做,因为它使代码更清洁),您可以使用标准JDK方法从json字符串中提取IP

If you want to forgo the creation of the intermediate object (which I do not propose you do since it makes the code cleaner), you could just extract the IP from the json String using standard JDK methods

这篇关于使用Spring获取RESTful Web服务以了解JSON字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆