【问题标题】:JSON doesn't get parsed correctly into databaseJSON 没有正确解析到数据库中
【发布时间】:2015-02-04 21:14:20
【问题描述】:

所以我为我的数据库创建了一个新实体,一切都很顺利,然后尝试将 JSON 解析为 DB,然后它没有正确解析它。正如您在 Controller 中看到的那样,我尝试打印 nazwa(name) 但它显示为 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
      */

道:

    @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 (?, ?)

【问题讨论】:

    标签: json spring hibernate spring-mvc


    【解决方案1】:

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

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

    有关详细信息,请参阅Spring docs。 @RequestBody 和 @ResponseBody 在 this thread 中有很好的描述。

    【讨论】:

    • 天啊,我还没有检查...我现在很尴尬...至少我知道我需要睡觉了,今天不用工作了,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-02
    • 1970-01-01
    相关资源
    最近更新 更多