我试图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
该应用程序没有针对/ error的显式映射,因此您将其视为后备。
星期二,十月09 13:51:00 IST 2018There是意外错误(类型=错误的请求,状态= 400)。不存在必需的请求部分“ json” <<<<<<<<<<<<<<<<<<<<<<<<< ERRORERRORERRORERRORERRORERRORERRORERRORERRORERROR400
我don't
想使用第三方okkhttp
等的依赖关系
我的逻辑错在哪里。请纠正我。为什么我不获得http
状态200
~~~~~~~~~~~~~问题似乎无法解决~~~~~~~~~~~~~
答案 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;
}
这也是一种解决方案,请尝试使用此