1

I am doing a Spring Boot project in Netbeans 8.2. In the front-end, i have used AngularJS. My server is Tommy 8 and i am using Maven. I have two problems.

  1. I use Postman to test my REST endpoint. For a post request i am sending a object and the Postman is returning message that the object has saved successfully. But when i have checked the database i have seen that a new record has saved with all fields zero except the auto generated primary key. I am using Hibernate. Here all the fields of that object is integer. I printed all the fields value of that object come from Postman, in the controller class. I have seen that all fields are showing zero. Then as a test i have set some static values in that object fields and sent to the persistence layer. Result is that the object has saved successfully in the database with the static values. I have faced same type of problem previously but i dont know how they have solved. In those times i have changed the fields name and solved the problem. This time i have changed the fields name but it is not working. In the previous problem only particular field was not working, not all the fields and the problem has not been happening all the times. But after changing the fields name every times it was working. I have done "clean and build" several times. I have the kept project in BitBucket. I have cloned again and done "clean and build" but the problem is same. I have cloned the project again in another folder and have removed the server and again added with a new username and password. The url is receiving data when i am sending them as request param. Below i have given the source codes.

Controller:

 @RequestMapping(value = "/addFollower", method = RequestMethod.POST)
    public Map services_addFollower(Follow follow)
    //public Map services_addFollower(@RequestParam int a,@RequestParam int b )
        {
            //Follow follow = new Follow();
            System.out.println("\n\nBefore printing value: ");
            System.out.println("A : "+follow.getColumnA());
            System.out.println("B : "+follow.getColumnB());
            //follow.setColumnA(2);
            //follow.setColumnB(3);
            //follow.setColumnA(a);
            //follow.setColumnB(b);
            System.out.println("\n\nBefore printing value: ");
            System.out.println("A : "+follow.getColumnA());
            System.out.println("B : "+follow.getColumnB());

            Map requestStatus_map = new HashMap();
            requestStatus_map.put("addFollower", services_Follow_I.services_addFollower(follow));
            return requestStatus_map;
        } 

Entity:

    public class Follow
    {


    private int follwerIndex;

    private int columnA;

    private int columnB;

    private String date = "Date";

    public int getFollwerIndex()
        {
        return follwerIndex;
        }

    public void setFollwerIndex(int follwerIndex)
        {
        this.follwerIndex = follwerIndex;
        }

    public int getColumnA()
        {
        return columnA;
        }

    public void setColumnA(int columnA)
        {
        this.columnA = columnA;
        }

    public int getColumnB()
        {
        return columnB;
        }

    public void setColumnB(int columnB)
        {
        this.columnB = columnB;
        }

    public String getDate()
        {
        return date;
        }

    public void setDate(String date)
        {
        this.date = date;
        }

    }

Persistence Layer :

public String dao_Generel_Insert(Object objectToInsert)
        {
        String dataInsertionStatus = " public String dao_Generel_Insert(Object objectToInsert) ";

        SessionFactory sessionFactory = null;
        Session session = null;

        try
          {
            sessionFactory = HibernateUtil.getSessionFactory();

            session = sessionFactory.openSession();

            Transaction tx = session.beginTransaction();
            session.save(objectToInsert);
              System.out.println("\n\n"+objectToInsert.getClass());
              tx.commit();

            dataInsertionStatus = "successInsert";
          } 
        catch (Exception e)
          {
            dataInsertionStatus = "failInsert";
            System.out.println("\n\nGenerel Insertion Error : " + e.getClass());
            System.out.println("Error occured in  : " + DAO_Generel_Insert_Update_Delete_SelectAll_Impl.class);
            System.out.println();
            e.printStackTrace();
            System.out.println();
            System.out.println();
            System.out.println("+++++++++++++++++++++++++++++ Insert error ++++++++++++++++++++++++++++++++++++++++");
          } 
        finally
          {
            session.close();
            sessionFactory.close();
            System.out.println("++++++++++++++++++++++++++++++  Insert +++++++++++++++++++++++++++++++++++++++");
          }

        return dataInsertionStatus;
        }
  1. When problem one happened then i have cloned the project again in another folder. I have removed the server and again added with a new username and password. I have created a new package and mapped some new urls in the new package. Then i have done "clean and build". When i am trying to access those urls from Postman it is showing error request url not found. I have checked the server log in the time of start, i have not found my new urls there. Then i have mapped a new url in my other previously existing and working packages and i found it is working. Same thing i have done that i have done start of problem two, cloning and rebuilding and reset the server. But no change. Even two url are with same name but it is not conflicting. I am checking server log i am not finding my new mapped urls in new package.

Controller :

@RestController
public class Controller_test 
    {

    @RequestMapping(value = "/testt", method = RequestMethod.POST)
    public void testt(@RequestBody Test t)
        {
            System.out.println("X : "+t.getX());
            System.out.println("Y : "+t.getY());
        }

    }

Entity :

public class Test
    {

    private int x;
    private int y;

    public int getX()
        {
        return x;
        }

    public void setX(int x)
        {
        this.x = x;
        }

    public int getY()
        {
        return y;
        }

    public void setY(int y)
        {
        this.y = y;
        }

    }
2
  • it would be helpful if you can share the persistence layer code which you use with hibernate. have you debug your code. Commented Jul 23, 2017 at 2:42
  • 1
    Would you please send us the request link and message ? Commented Jul 23, 2017 at 7:41

1 Answer 1

1

If you want to send data as json request message with the link

http://localhost:8080/addFollower

Then change the controller function as :

@RequestMapping(value = "/addFollower", method = RequestMethod.POST)
public Map services_addFollower(@RequestBody Follow follow) {
 // your code goes here
}

By default, @RequestBody takes message as json format.

In your given code, you didn't add any message format. That's why you need to add data as query parameter. Like

http://localhost:8080/addFollower?columnA=1&columnB=2

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.