无法发布多部分表单数据json请求

时间:2018-10-09 06:50:25

标签: java spring-boot

我试图post multipart form-data用Java向api请求。我收到400 http错误代码。下面是我在春天的终点:

spring boot api

@org.springframework.web.bind.annotation.PostMapping(value="register2")
public ResponseEntity<CoreResponseHandler> doRegister2(@RequestPart String json)  { 
System.out.println("~~~~~~     "+json+"   ~~~~~~"); 
    return null;
}

要在api上使用的核心Java代码

  import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.JSONException;
import org.json.JSONObject;
public class Test {
    public static void main(String[] args) {
        try {
            URL url = new URL("http://localhost:8012/register2");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
            conn.setRequestProperty("charset","utf-8");
            conn.setRequestProperty("Content-Disposition","form-data;name='json'");
            conn.setDoOutput(true);
            conn.setDoInput(true);
            JSONObject jsonObject = buidJsonObject();
            setPostRequestContent(conn, jsonObject);
            conn.connect();
            System.out.println(conn.getResponseCode());
            if(conn.getResponseCode()==200){
                BufferedReader reader= new BufferedReader(new InputStreamReader(conn.getInputStream()));
                StringBuilder stringBuilder = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null)
                {
                    stringBuilder.append(line + "\n");
                }
                System.out.println(stringBuilder+" <<<<<<<<<<<<<<<<<<<<<<<<<" );
                jsonObject=new JSONObject(stringBuilder.toString());    
            }
            else{
                BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
                StringBuilder stringBuilder = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null)
                {
                    stringBuilder.append(line + "\n");
                }
                System.out.println(stringBuilder+" <<<<<<<<<<<<<<<<<<<<<<<<<" );
                System.out.println("ERRORERRORERRORERRORERRORERRORERRORERRORERROR"+conn.getResponseCode());
            }

        }catch(Exception ex) {
            ex.printStackTrace();
        }

    }

    private static  JSONObject buidJsonObject() throws JSONException {

        JSONObject jsonObject = new JSONObject();
        jsonObject.accumulate("username", "13338912");
        jsonObject.accumulate("encryptedPassword", "encpass");
        jsonObject.accumulate("name",  "fish");
        jsonObject.accumulate("email",  "fish@fish.com");
        jsonObject.accumulate("status",  "active");


        return jsonObject;
    }

    private static void setPostRequestContent(HttpURLConnection conn, 
            JSONObject jsonObject) throws IOException {

        OutputStream os = conn.getOutputStream();
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
        writer.write(jsonObject.toString());
        writer.flush();
        writer.close();
        os.close();
    }

}

下面是我得到的输出:

输出

400

Whitelabel错误页面

该应用程序没有针对/ error的显式映射,因此您将其视为后备。

星期二,十月09 13:51:00 IST 2018There是意外错误(类型=错误的请求,状态= 400)。不存在必需的请求部分“ json”  <<<<<<<<<<<<<<<<<<<<<<<<< ERRORERRORERRORERRORERRORERRORERRORERRORERRORERROR400

don't想使用第三方okkhttp等的依赖关系

我的逻辑错在哪里。请纠正我。为什么我获得http状态200

~~~~~~~~~~~~~问题似乎无法解决~~~~~~~~~~~~~

1 个答案:

答案 0 :(得分:0)

@org.springframework.web.bind.annotation.PostMapping(value="register2" ,consumes = MediaType.APPLICATION_JSON, 
                produces = MediaType.APPLICATION_JSON)
public ResponseEntity<CoreResponseHandler> doRegister2(@RequestPart String json)  { 
System.out.println("~~~~~~     "+json+"   ~~~~~~"); 
    return null;
}

这也是一种解决方案,请尝试使用此