0

So i made a new Entity for my database, everything was going great, then tried parsing JSON into DB, and then it didn't parse it correctly. As You can see in Controller i've tried printing nazwa(name) but it showed null, don't know why. I've checked if there is any kind of errors with spelling with JSON request but it is one simple property (name). Made so many Entities with all stuff but i don't know what's wrong over here.

This is a POST request at http://localhost:8080/api/typzgloszenia (Using POSTMAN) :

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

I am getting this :

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

Here is Controller :

    @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 :

    @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
     */

and Entity :

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;
        }
    }

Log :

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 (?, ?)
papski
  • 37
  • 1
  • 2
  • 7

1 Answers1

1

Try to use @RequestBody annotation in controller. Like this:

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

For more information see Spring docs. @RequestBody and @ResponseBody are well described in this thread.

Community
  • 1
  • 1
kurochenko
  • 1,214
  • 3
  • 19
  • 43
  • OMG, I haven't checked that... I'm so embarrassed right now... At least i know i need to go sleep, no more work for today, thanks! – papski Feb 04 '15 at 21:28