在java webservice中使用一组json对象

时间:2017-11-02 21:08:51

标签: java json jersey

我有一个带有Jersey和maven的java web服务,它通过来自我的角度前端的HTTP帖子获取一个json对象数组。我的插入方法适用于1 json对象,但现在我需要发送一个数组,并在tryng时获得错误400。我失踪了什么?

这是HTTP帖子

    postNeuromotora(params){
    let headers = new Headers();
    headers.append('Content-type','application/json');
    return this.http.post(`${this.api}neuromotora/`,params,{
      headers: headers
  }).map(
    (res:Response) => {return res.json();}
  );

  }

这是INSERT java方法

public void insert(Neuromotora[] array) throws SQLException, ClassNotFoundException{
            Long id = null;
            String sqlQuery = "INSERT INTO neuromotora(nervonome,latencia,amplitudedistal,amplitudeprox,velocidade,ondaf,pacienteid,ladonervo)"
                    + " VALUES(?,?,?,?,?,?,?,?) RETURNING neuromotoraid";
            for(Neuromotora oferta : array ){
                   try{
                PreparedStatement stmt = this.con.getConnection().prepareStatement(sqlQuery);
                stmt.setString(1,oferta.getNervoNome());
                stmt.setString(2,oferta.getLatencia());
                stmt.setString(3,oferta.getAmplitudeDistal());
                stmt.setString(4,oferta.getAmplitutdeProx());
                stmt.setString(5, oferta.getVelocidade());
                stmt.setString(6, oferta.getOndaF());
                stmt.setInt(7,oferta.getPacienteid());
                stmt.setString(8,oferta.getLadoNervo());


                ResultSet rs = stmt.executeQuery();
                if(rs.next()){
                    id = rs.getLong("neuromotoraid");
                }

                this.con.commit();        
                }
            catch(SQLException e){
                this.con.rollback();
                throw e;
                }
            }



                }        

这是我控制器上的post方法

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Path("/neuromotora/")
    public Response createNeuromotora(Neuromotora[] n) throws SQLException, ClassNotFoundException{
        neuromotoraDAO dao = new neuromotoraDAO();

        dao.insert(n);

        return Response
            .status(200)
            .header("Access-Control-Allow-Origin", "*")
            .header("Access-Control-Allow-Headers", "origin, content-type, accept, authorization")
            .header("Access-Control-Allow-Credentials", "true")
            .header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD")
            .header("Access-Control-Max-Age", "1209600")
            .entity(n)
            .build();

    }

这是我通过post发送的json数组的输出(只写了2个属性,因为我无法复制所有这些)

[{" pacienteid":5," nervonome":""},{" pacienteid":5,&#34 ; nervonome":""}]

http:https://pastebin.com/AEjrmq2K

的Stacktrace

0 个答案:

没有答案