JSON无法正确解析到数据库中

时间:2015-02-04 21:14:21

标签: json spring hibernate spring-mvc

所以我为我的数据库创建了一个新实体,一切都很好,然后尝试将JSON解析为DB,然后它没有正确解析它。正如您在Controller中所看到的那样,尝试打印nazwa(名称),但它显示为null,不知道为什么。我已经检查了JSON请求拼写是否有任何错误,但它是一个简单的属性(名称)。用这些东西制作了这么多实体,但我不知道这里有什么错误。

这是http://localhost:8080/api/typzgloszenia的POST请求(使用POSTMAN):

{
    "nazwa" : "test"      //name
}

我得到了这个:

{
"idTypuZgloszenia": 1,     // id of request type
"nazwa": null              //name
}

这是控制器:

    @Controller
    @RequestMapping("/typzgloszenia")
    public class TypZgloszeniaController {

        private iTypZgloszeniaService itypZgloszeniaService;

        @Autowired
        public TypZgloszeniaController (iTypZgloszeniaService itypZgloszeniaService) {
            this.itypZgloszeniaService = itypZgloszeniaService;
        }

        @RequestMapping(method = RequestMethod.POST)
        public ResponseEntity<TypZgloszeniaEntity> addRequest(TypZgloszeniaEntity typZgloszeniaEntity) {
            System.out.println(typZgloszeniaEntity.getNazwa());   //shows null...
            TypZgloszeniaEntity addRequest = itypZgloszeniaService.addRequest(typZgloszeniaEntity);
            if (addRequest !=null) {
                return new ResponseEntity<TypZgloszeniaEntity>(addRequest, HttpStatus.OK);
            } else {
                return new ResponseEntity<TypZgloszeniaEntity>(HttpStatus.NOT_FOUND);
            }
        }

      /*
      MUCH MORE STUFF
      */

服务:

    @Service
    @Transactional
    public class TypZgloszeniaService implements iTypZgloszeniaService {

        @Autowired
        private iTypZgloszeniaDAO itypZgloszeniaDAO;

        @Override
        public TypZgloszeniaEntity addRequest(TypZgloszeniaEntity typZgloszeniaEntity) {
            return itypZgloszeniaDAO.addRequest(typZgloszeniaEntity);
        }

      /*
      MUCH MORE STUFF
      */

DAO:

    @Repository
    public class TypZgloszeniaDAO implements iTypZgloszeniaDAO {

        @PersistenceContext
        EntityManager em;

        @Override
        public TypZgloszeniaEntity addRequest(TypZgloszeniaEntity typZgloszeniaEntity) {
            em.persist(typZgloszeniaEntity);
            return typZgloszeniaEntity;
        }

     /*
     MUCH MORE STUFF
     */

和实体:

package praktyki.core.entities;

import javax.persistence.*;

/**
 * Created by dawid on 04.02.15.
 */

    @Entity
    @Table(name = "typ_zgloszenia", schema = "public", catalog = "praktykidb")
    public class TypZgloszeniaEntity {
        private Integer idTypuZgloszenia; // id of request type
        private String nazwa;             //name

        /*
        ATRYBUTY
        */

        @Id
        @GeneratedValue
        @Column(name = "id_typu_zgloszenia") // id of request type
        public Integer getIdTypuZgloszenia() {
            return idTypuZgloszenia;
        }

        public void setIdTypuZgloszenia(Integer idTypuZgloszenia) {
            this.idTypuZgloszenia = idTypuZgloszenia;
        }

        @Basic
        @Column(name = "nazwa") //name
        public String getNazwa() {
            return nazwa;
        }

        public void setNazwa(String nazwa) {
            this.nazwa = nazwa;
        }

        /*
        EQUALS I HASHCODE
        */

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (!(o instanceof TypZgloszeniaEntity)) return false;

            TypZgloszeniaEntity that = (TypZgloszeniaEntity) o;

            if (idTypuZgloszenia != null ? !idTypuZgloszenia.equals(that.idTypuZgloszenia) : that.idTypuZgloszenia != null)
                return false;
            if (nazwa != null ? !nazwa.equals(that.nazwa) : that.nazwa != null) return false;

            return true;
        }

        @Override
        public int hashCode() {
            int result = idTypuZgloszenia != null ? idTypuZgloszenia.hashCode() : 0;
            result = 31 * result + (nazwa != null ? nazwa.hashCode() : 0);
            return result;
        }
    }

记录:

null
Hibernate: select next_hi from hibernate_unique_key for update
Hibernate: update hibernate_unique_key set next_hi = ? where next_hi = ?
Hibernate: insert into praktykidb.public.typ_zgloszenia (nazwa, id_typu_zgloszenia) values (?, ?)

1 个答案:

答案 0 :(得分:1)

尝试在控制器中使用@RequestBody注释。像这样:

 public ResponseEntity<TypZgloszeniaEntity> addRequest(@RequestBody TypZgloszeniaEntity typZgloszeniaEntity) {

有关详细信息,请参阅Spring docsthis thread中详细描述了@RequestBody@ResponseBody