gpt4 book ai didi

java - 解决Rest API调用状态码400问题

转载 作者:行者123 更新时间:2023-12-02 10:19:52 24 4
gpt4 key购买 nike

实际上我知道这是因为错误的请求而发生的。但我无法弄清楚问题所在。我创建了一个 java 类来调用接受 JSON 对象并返回 JSON 作为响应的 REST API。以下是我的JAVA代码

public static void dataTest() {
List<String> resultSet = new ArrayList<>();
try {
URL url = new URL("http://localhost:8080/login/");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
String input = "{\"mobile_number\":0719402232,\"pin\":\"1111\"}";
OutputStream os = conn.getOutputStream();
os.write(input.getBytes());
os.flush();

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) {
resultSet.add(output);
System.out.println(output);
}
conn.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}

但我收到 400 状态代码。

而且我还需要将返回的 JSON 传递给 JSP。以下是我访问JAVA类的方式

  <ul>
<c:forEach items="${AddOrigin.DataTest()}" var="ListItem">
<li>${ListItem.name}</li>
</c:forEach>
</ul>

在 JSP 中查看以下错误

org.apache.jasper.JasperException: An exception occurred processing [/dwpages/AddOrigin.jsp] at line [359]

356: <div id="main-content"></div>
357:
358: <ul>
359: <c:forEach items="${AddOrigin.DataTest()}" var="ListItem">
360: <li>${ListItem.name}</li>
361: </c:forEach>
362:

以下是登录 Controller

@RequestMapping(value = "/login/", method = RequestMethod.POST)
public Origin Login(HttpServletRequest _request, @RequestBody LoginVM loginVM, HttpServletResponse response)
throws Exception {
String _reqDtil = _requestDetails.getRequestDetails(_request);
//LogUtil.getLog("Authenticate").debug(_reqDtil + ",AUTHENTICATION,Start,Request="+loginVM.toString());
Logger.getLogger("Authenticate").debug(_reqDtil + "AUTHENTICATION,Start,Request="+loginVM.toString());
Origin _origin = null;
try {
_origin = _authentication.authenticate(loginVM.getMobile_number(), loginVM.getPin());
} catch (DWException ex) {
Logger.getLogger("Authenticate").debug(_reqDtil + "AUTHENTICATION,Exception,Request=" + loginVM.getMobile_number() + ","
+ loginVM.getPin() + "," + _requestDetails.getErrorString(ex));
// LogUtil.getLog("Authenticate")
// .debug(_reqDtil + ",AUTHENTICATION,Exception,Request=" +
loginVM.getMobile_number() + ","
// + loginVM.getPin() + "," +
_requestDetails.getErrorString(ex));
response.sendError(ex.getErrorCode(), ex.getErrorMessage());
}
return _origin;

}

这是我的登录虚拟机

public class LoginVM {
private String mobile_number;
private int pin;

public String getMobile_number() {
return mobile_number;
}
public void setMobile_number(String mobile_number) {
this.mobile_number = mobile_number;
}
public int getPin() {
return pin;
}
public void setPin(int pin) {
this.pin = pin;
}
@Override
public String toString() {
return "LoginVM [mobile_number=" + mobile_number + ", pin=" + pin + "]";
}

}

这是我的原始模型

@Entity
@Table(name = "origin")
public class Origin {
@Id
@Column(name = "acc_no", nullable = false)
private String acc_no;

@Column(name = "pan_no", nullable = true)
private String pan_no;

@Column(name = "mobile_number", nullable = true)
private String mobile_number;

@Column(name = "name", nullable = true)
private String name;

@Column(name = "nic", nullable = true)
private String nic;

@Column(name = "dob", nullable = true)
private Date dob;

@Column(name = "gender", nullable = true)
private String gender;

@Column(name = "address", nullable = true)
private String address;

@Column(name = "pin", nullable = true)
private int pin=1111;

@Column(name = "nominee", nullable = true)
private String nominee;

@Column(name = "nominee_nic", nullable = true)
private String nominee_nic ;

@Column(name = "created_date", nullable = true)
private Date created_date=new Date();

@Column(name = "super_mobile_number", nullable = false)
private String super_mobile_number;

@Column(name = "verification", nullable = true)
private int verification=0;

@ManyToOne
private Wallet wallet;

@ManyToOne
private OriginTypes origin_types;

@ManyToOne
private MembershipTypes membership_types;

@ManyToOne
private Status status;



public String getPan_no() {
return pan_no;
}

public void setPan_no(String pan_no) {
this.pan_no = pan_no;
}

public String getAcc_no() {
return acc_no;
}

public void setAcc_no(String acc_no) {
this.acc_no = acc_no;
}

public String getMobile_number() {
return mobile_number;
}

public void setMobile_number(String mobile_number) {
this.mobile_number = mobile_number;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getNic() {
return nic;
}

public void setNic(String nic) {
this.nic = nic;
}

public Date getDob() {
return dob;
}

public void setDob(Date dob) {
this.dob = dob;
}

public String getGender() {
return gender;
}

public void setGender(String gender) {
this.gender = gender;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public int getPin() {
return pin;
}

public void setPin(int pin) {
this.pin = pin;
}

public String getNominee() {
return nominee;
}

public void setNominee(String nominee) {
this.nominee = nominee;
}

public String getNominee_nic() {
return nominee_nic;
}

public void setNominee_nic(String nominee_nic) {
this.nominee_nic = nominee_nic;
}

public Date getCreated_date() {
return created_date;
}

public void setCreated_date(Date created_date) {
this.created_date = created_date;
}

public String getSuper_mobile_number() {
return super_mobile_number;
}

public void setSuper_mobile_number(String super_mobile_number) {
this.super_mobile_number = super_mobile_number;
}

public Wallet getWallet() {
return wallet;
}

public void setWallet(Wallet wallet) {
this.wallet = wallet;
}

public OriginTypes getOrigin_types() {
return origin_types;
}

public void setOrigin_types(OriginTypes origin_types) {
this.origin_types = origin_types;
}

public MembershipTypes getMembership_types() {
return membership_types;
}

public void setMembership_types(MembershipTypes membership_types) {
this.membership_types = membership_types;
}

public Status getStatus() {
return status;
}

public void setStatus(Status status) {
this.status = status;
}

public int getVerification() {
return verification;
}

public void setVerification(int verification) {
this.verification = verification;
}


}

我的代码有什么问题。请帮助我修复错误。

最佳答案

我认为您应该将电话号码作为字符串传递(在引号之间)

关于java - 解决Rest API调用状态码400问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54415662/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com