JSON post to Spring Controller

前端 未结 5 1504
清酒与你
清酒与你 2020-12-24 08:50

Hi I am starting with Web Services in Spring, so I am trying to develop small application in Spring + JSON + Hibernate. I have some problem with HTTP-POST. I created a metho

5条回答
  •  北荒
    北荒 (楼主)
    2020-12-24 09:26

    Do the following thing if you want to use json as a http request and response. So we need to make changes in [context].xml

    
    
        
            
                
            
        
    
    
    
       
    

    MappingJackson2HttpMessageConverter to the RequestMappingHandlerAdapter messageConverters so that Jackson API kicks in and converts JSON to Java Beans and vice versa. By having this configuration, we will be using JSON in request body and we will receive JSON data in the response.

    I am also providing small code snippet for controller part:

        @RequestMapping(value = EmpRestURIConstants.DUMMY_EMP, method = RequestMethod.GET)
    
        public @ResponseBody Employee getDummyEmployee() {
        logger.info("Start getDummyEmployee");
        Employee emp = new Employee();
        emp.setId(9999);
        emp.setName("Dummy");
        emp.setCreatedDate(new Date());
        empData.put(9999, emp);
        return emp;
    }
    

    So in above code emp object will directly convert into json as a response. same will happen for post also.

提交回复
热议问题