【问题标题】:why don't you see the "keys" in the json?为什么在 json 中看不到“键”?
【发布时间】:2020-01-16 01:00:44
【问题描述】:

当我输入邮递员时,我得到了 json,但没有他的“钥匙”,为什么?也许我犯了一个错误,我没有注意到。请帮忙。

我正在使用存储过程来做一些事情。

这是向我展示邮递员的 json。显示我没有他的“钥匙”

{
    "data": [
        [
            1,
            "aaa",
            "aaa@gmail.com"
        ],
        [
            2,
            "bbb",
            "bbb@gmail.com"
        ],
        [
            3,
            "ccc",
            "ccc@gmail.com"
        ]
    ]
}

我想要这样的东西。

{
    "data": [
        {
          userCod: 1,
          userName: "aaa",
          userEmail: "aaa@gmail.com"
        },
        {
          userCod: 2,
          userName: "bbb",
          userEmail: "bbb@gmail.com"
        },
        {
          userCod: 3,
          userName: "ccc",
          userEmail: "ccc@gmail.com"
        }
    ]
}

我留下代码

public class ApiResponse {
    private List<UserTest> data;

    public List<UserTest> getData() {
        return data;
    }

    public void setData(List<UserTest> data) {
        this.data = data;
    }

}
@Entity
@Table(name = "tbUsers")
public class UserTest implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "userCod")
    private Long id;
    @Column(name = "userName")
    private String name;
    @Column(name = "userEmail")
    private String email;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

}
@Repository
public class ClienteDaoImpl implements IClienteDao{

    @Autowired
    private EntityManager em;

    @SuppressWarnings("unchecked")
    @Override
    public ApiResponse mntUsers(int op) {
        ApiResponse api = new ApiResponse();
        Session session = em.unwrap(Session.class);
        ProcedureCall call = session.createStoredProcedureCall("sp_MntUser");
        call.registerParameter(1, Integer.class, ParameterMode.IN);
        call.setParameter(1, op);
        call.execute();
        api.setData(call.getResultList());
        return api;
    }

}
@RestController
@RequestMapping(value = "/mntUsers")
public class ClienteController {

    @Autowired
    private ClienteServiceImpl serviceImpl;

    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<?> CrudUsers(@RequestParam(value = "option", required = true) Integer op) {
        return new ResponseEntity<>(serviceImpl.mntUsers(op),HttpStatus.OK);
    }

}

【问题讨论】:

  • call.getResultList() 返回a array of objects 然后你总是会得到你的第一个响应,如果你想转换那么你应该将每个对象数组转换为UserTest
  • 你能举个例子吗?
  • 您可以尝试使用@JSONProperty 注解进行 JSON 序列化吗?
  • 我试过了,但是@JsonProperty 不起作用。
  • @makoto 我写了一个答案,检查一下!

标签: java spring-boot stored-procedures spring-data-jpa


【解决方案1】:

创建一个名为getCollectionType的方法

public static <T, C extends Collection<T>> C getCollectionType(Iterable<?> from, C to, Class<T> listClass) {
    for (Object item: from) {
        to.add(listClass.cast(item));
    }
    return to;
}

然后在下面一行使用它:

api.setData(getCollectionType(call.getResultList(), 
                              new ArrayList<UserTest>(), 
                              UserTest.class));

【讨论】:

  • 它不起作用。我收到此错误:“无法将 [Ljava.lang.Object; 转换为 com.proyecto.app.entity.UserTest”
  • 我需要有一个“用户”实体吗?如您所见,我创建了一个实体“ApiResponse”,将存储过程生成的所有结果存储在我的属性“data”中
  • 是的,我想是的,因为从存储过程中检索时,您会收到您提到的错误...
  • 得到一个json结果就这么难吗?还是我的代码很糟糕?请帮忙。用c#简单多了,为什么java这么复杂?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-02
  • 1970-01-01
  • 2022-08-14
  • 1970-01-01
  • 1970-01-01
  • 2021-07-21
  • 1970-01-01
相关资源
最近更新 更多