Wednesday, 3 April 2013

Rest Template Json Request

On the server side
The controller:
@RequestMapping( value="/user/save", method=RequestMethod.POST, consumes="application/json" )
public @ResponseBody User saveUser( @RequestBody User user ) {
  
    userService.saveUser( user );
  
    return user;

}
servlet-context.xml:



 
 
 
 

             
 
 
 
 

 
 
 
 
 
     
     
 
 
 
 
         
                 
                     
                
        
 

 
 
        
 
 
On the client side The test:
package com.rest.client.test;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.client.RestTemplate;



@RunWith( SpringJUnit4ClassRunner.class )
@ContextConfiguration( "classpath:META-INF/conf/test-conf.xml" )
public class RestWebServiceTest {

    @Autowired
    private RestTemplate restTemplate;
 
 
    @Test
    public void shouldSaveJsonStringUser() {

        String jsonStringUser = "{"
        + "\"id\":\"\"" + ","
        + "\"firstName\":\"nicolas\"" + ","
        + "\"lastName\":\"loriente\""
        + "}";

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType( MediaType.APPLICATION_JSON );

        HttpEntity request= new HttpEntity( jsonStringUser, headers );
  
        User returnedUser = restTemplate.postForObject( "http://localhost:8080/Rest/user/save", request, User.class );
  
        // user id is populated when saved to DB
        assertNotNull( returnedUser.getId() );
        assertEquals( "nicolas", returnedUser.getFirstName() );
        assertEquals( "loriente", returnedUser.getLastName() );

    }
 
    @Test
    public void shouldSaveObjectUser() {

        User user = new User();
        user.setFirstName( "nicolas" );
        user.setLastName( "loriente" );

        User returnedUser = restTemplate.postForObject( "http://localhost:8080/Rest/user/save", user, User.class );
  
        // user id is populated when saved to DB
        assertNotNull( returnedUser.getId() );
        assertEquals( "nicolas", returnedUser.getFirstName() );
        assertEquals( "loriente", returnedUser.getLastName() );

    }
 
}
test-conf.xml: Code:


 
 
 
  
 
 
         
                 
                     
                
        
 
 

1 comments:

  1. Hi,
    The prototype of HttpEntity is not matching with what has been used.I am getting error Multiple markers at this line
    - HttpEntity is a raw type. References to generic type HttpEntity should be parameterized
    - The type org.springframework.util.MultiValueMap cannot be resolved. It is indirectly
    referenced from required .class files
    - HttpEntity is a raw type. References to generic type HttpEntity should be parameterized
    - The constructor HttpEntity(String, HttpHeaders) is undefined

    on that line.

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...