创建一个JavaREST客户机来调用SpringBootRESTAPI

创建一个JavaREST客户机来调用SpringBootRESTAPI,java,spring-boot,Java,Spring Boot,我有一个springboot项目,它只接受POST JSON字符串,我正在使用gson将其转换为HashMap。我使用Postman作为POST进行了测试,并将主体添加为props,其中包含一个json字符串,如{'fistname':'John','lastname':'Doe'},翻译为props={'fistname':'John','lastname':'Doe'}。工作如期进行 @RequestMapping(value = "/rest", method = RequestMetho

我有一个springboot项目,它只接受POST JSON字符串,我正在使用gson将其转换为HashMap。我使用Postman作为POST进行了测试,并将主体添加为
props
,其中包含一个json字符串,如
{'fistname':'John','lastname':'Doe'}
,翻译为
props={'fistname':'John','lastname':'Doe'}
。工作如期进行

@RequestMapping(value = "/rest", method = RequestMethod.POST)
    protected String parse(@RequestParam("props") String props) {
    Gson gson = new Gson();
    Map<String, String> params = new HashMap<String, String>();
    params = gson.fromJson(props, Map.class);

    // Rest of the process
}
@RequestMapping(value=“/rest”,method=RequestMethod.POST)
受保护的字符串分析(@RequestParam(“props”)字符串props){
Gson Gson=新的Gson();
Map params=新的HashMap();
params=gson.fromJson(props,Map.class);
//过程的其余部分
}
另一方面,我有一个JavaEE项目,它需要调用这个API

protected void callREST() {

      try {
            String json = someClass.getDate() //retrieved from database which is stored as json structure
            Map<String, String> props = gson.fromJson(json, Map.class);

            URL url = new URL("http://localhost:9090/myApp/rest");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/json");

            DataOutputStream wr = new DataOutputStream( conn.getOutputStream());

            System.out.println(props.toString());
            wr.writeBytes(json.toString());
            wr.flush();
            wr.close();
            if(conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
                throw new RuntimeException("Failed :: HTTP error code : " + conn.getResponseCode());
            }

            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String output;
            System.out.println("Output from Server ... \n");
            while((output = br.readLine()) != null) {
                System.out.println(output);
            }

            conn.disconnect();

       } catch(Exception e) {
          //print stack trace
       }
 }
protectedvoid callREST()受保护{
试一试{
String json=someClass.getDate()//从存储为json结构的数据库中检索
Map props=gson.fromJson(json,Map.class);
URL=新URL(“http://localhost:9090/myApp/rest");
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
连接设置输出(真);
conn.setRequestMethod(“POST”);
conn.setRequestProperty(“内容类型”、“应用程序/json”);
DataOutputStream wr=新的DataOutputStream(conn.getOutputStream());
System.out.println(props.toString());
writeBytes(json.toString());
wr.flush();
wr.close();
if(conn.getResponseCode()!=HttpURLConnection.HTTP_已创建){
抛出新的RuntimeException(“失败的::HTTP错误代码:+conn.getResponseCode());
}
BufferedReader br=新的BufferedReader(新的InputStreamReader(conn.getInputStream());
字符串输出;
System.out.println(“从服务器输出…\n”);
而((output=br.readLine())!=null){
系统输出打印项次(输出);
}
连接断开();
}捕获(例外e){
//打印堆栈跟踪
}
}
我获取
失败::HTTP错误代码:400
。我怀疑spring boot没有收到
props
变量中的数据,因为它是POST请求。 我应该在客户端代码中添加什么来传递道具和数据,以使此调用成功

注意:JavaEE在tomcat:8080上运行,Springboot在 不同的雄猫:9090


@RequestParam
表示服务器在请求URL中等待param
http://localhost:9090/myApp/rest?param=.....
但在客户机中,您正在请求体中编写JSON

尝试在端点中使用
@RequestBody
注释

protected String parse(@RequestBody String props) {...}

@RequestParam
表示服务器在请求URL中等待param
http://localhost:9090/myApp/rest?param=.....
但在客户机中,您正在请求体中编写JSON

尝试在端点中使用
@RequestBody
注释

protected String parse(@RequestBody String props) {...}

您的资源希望获得表单参数(即使用x-www-form-urlencoded编码的键值对),其中的值恰好是JSON(尽管您发布的不是有效的JSON)

但是您的客户机Java代码将内容类型设置为application/json,并将json作为主体发送,而不是作为x-www-form-urlencoded主体的键“props”的值发送

所以那是行不通的

如果您可以更改服务器,那么就这样做。直接接受JSON作为主体:

@RequestMapping(value = "/rest", method = RequestMethod.POST)
public String parse(@RequestBody Map<String, String> map) {
     ...
}
@RequestMapping(value=“/rest”,method=RequestMethod.POST)
公共字符串解析(@RequestBody映射){
...
}

如果没有,您需要发送正确的键值对,并确保该值是正确的url编码。

您的资源希望获得表单参数(即键值对,使用x-www-form-urlencoded编码),其中值恰好是JSON(尽管您发布的不是有效的JSON)

但是您的客户机Java代码将内容类型设置为application/json,并将json作为主体发送,而不是作为x-www-form-urlencoded主体的键“props”的值发送

所以那是行不通的

如果您可以更改服务器,那么就这样做。直接接受JSON作为主体:

@RequestMapping(value = "/rest", method = RequestMethod.POST)
public String parse(@RequestBody Map<String, String> map) {
     ...
}
@RequestMapping(value=“/rest”,method=RequestMethod.POST)
公共字符串解析(@RequestBody映射){
...
}

如果没有,您需要发送正确的键值对,并确保该值是正确的url编码。

查看Spring的
RestTemplate
类,这非常有用。查看Spring的
RestTemplate
类,这非常有用。如何使其异步?如何使其异步?