简体   繁体   English

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

[英]Resolve Rest API call status code 400 Problem

Actually I know this happen because of bad request. 实际上,我知道这是由于错误的请求而发生的。 But I cannot figure out the problem. 但我无法弄清楚问题。 I have created a java class to call a rest API that accept JSON object and return A JSON as a response. 我创建了一个Java类来调用可以接受JSON对象并返回JSON作为响应的rest API。 Following is my JAVA code 以下是我的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();
    }
}

But I am getting 400 Status code. 但是我得到了400状态代码。

And also I need to pass the returned JSON to the JSP. 而且我还需要将返回的JSON传递给JSP。 Following is the way I access the JAVA class 以下是我访问JAVA类的方式

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

Following error viewed in JSP 在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: 

Following is the login 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;

}

And this is my Login VM 这是我的登录虚拟机

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 + "]";
}

}

And this is my Origin Model 这是我的原产地模型

@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;
}


}

What is the problem in my code.Please help me to fix the error. 我的代码有什么问题。请帮助我修复错误。

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

I my java code I have Following line 我的java代码中有以下行

String input = "{\"mobile_number\":0719402232,\"pin\":\"1111\"}";

It must be changed to 必须将其更改为

String input = "{\"mobile_number\":\"0719402232\",\"pin\":\"1111\"}";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM