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

时间:2019-01-29 07:12:58

标签: java rest jsp

实际上,我知道发生这种情况是因为要求不高。但我无法弄清楚问题。我创建了一个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: 			

以下是登录控制器

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


}

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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

我的Java代码中有以下行

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

必须更改为

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