简体   繁体   English

arraylist / list RESTful Web服务json

[英]arraylist/list RESTful web service json

Hello I want to ask about web service, how to show values from database to Map<>? 您好我想问一下Web服务,如何显示从数据库到Map <>的值?

here is my code 这是我的代码

    @GET
    @Path("/jurusan/{kode_jurusan}")
    @Produces(MediaType.APPLICATION_JSON)    
    public Map getMatkulByjurusan(@PathParam("kode_jurusan") String kode_jurusan){
        Map<String, Object> rs = new HashMap<String, Object>();
        rs.put("Pesan", "Berhasil");
        System.out.println("Jurusan "+kode_jurusan);
        try {
            createConnection();
            MyMap matkul =(MyMap) jt.queryObject("select matkul from tb_matkul where kode_jurusan = ?", new Object[] {kode_jurusan}, new MyMap());
            closeConnection();
            if(matkul != null){
                rs.put("result", matkul);
            }
        } catch (Exception e) {
            rs.put("Pesan", "Gagal karena : " +e.getMessage());
        }
        return rs;
    }

}

but when I try to acess http://localhost:9090/Service/matkul/jurusan/40 I get the following message: 但是当我尝试访问http:// localhost:9090 / Service / matkul / jurusan / 40时,我收到以下消息:

{"Pesan":"Gagal karena : Incorrect result size: expected 1, actual 14"}

this MyMap class 此MyMap类

public class MyMap implements Serializable, RowMapper{
    private static final long serialVersionUID = -8840406844877458198L;
    public HashMap<String, Object> map = new HashMap<String, Object>();

    public HashMap<String, Object> getMap() {
        return map;
    }

    public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
        MyMap dto=new MyMap();
        int rowCount = rs.getMetaData().getColumnCount();
        for (int i = 1; i <= rowCount; i++) {
            dto.map.put(rs.getMetaData().getColumnLabel(i), rs.getObject(i));
        }
        return dto;
    }

    public void put(String name, Object o){
        map.put(name, o);
    }

    public Object get(String name){
        return map.get(name);
    }

    public String getString(String name){
        return (String)map.get(name);
    }

    public Integer getInt(String name){
        return (Integer)map.get(name);
    }

    public Date getDate(String name){
        return (Date)map.get(name);
    }

    public BigDecimal getBigDecimal(String name){
        return (BigDecimal)map.get(name);
    }


}

Use queryForList method instead queryObject . 使用queryForList方法代替queryObject

you can find an example to map multiple rows to list using jdbcTemplate from here . 您可以从此处找到使用jdbcTemplate将多行映射到列表的示例。

Looks like the problem is on the database query, as the exception says, the query is expecting only 1 row as result and it produces 14. 看起来问题出在数据库查询上,如异常所示,该查询期望结果只有1行,并产生14行。

Moreover, depending on which framework are you using you should probably provide a way to serialize the MyMap class 此外,根据您使用的框架,您可能应该提供一种序列化MyMap类的方法

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

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