springspring-mvcspring-android

Spring-MVC : Creating restful controller for posting/retreival of Objects


I am working on an Android application with which I want to connect to a Spring-MVC based webapp. Currently I am working on building a restful controller for the webapp. I tried connecting via Android to the controller method to save an object which I am passing. A null row (with Id ofcourse) in database was created. I can use some help in knowing how can I send Java objects which can be persisted, and retrieval of object/s. Here is what I have so far :

Current controller :

    @RequestMapping(value = "/restaurant/add",method = RequestMethod.POST)
    @ResponseStatus( HttpStatus.CREATED )
    @ResponseBody
    public String addRestaurantWebView(Restaurant restaurant){
           System.out.println("Restaurant details are"+restaurant.getRestaurantName());
        return "true";
    }

Spring bean :  


<beans:bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <beans:property name="messageConverters">
               <beans:ref bean="jsonMessageConverter"/>
        </beans:property>
    </beans:bean>

    <beans:bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>

Restaurant Entity :

@Entity
@Table(name="restaurant")
public class Restaurant implements UserDetails{

private static final GrantedAuthority USER_AUTH = new SimpleGrantedAuthority("ROLE_RESTO");

private static final String emailRegexp = "^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+$";

@Id
@Column(name="restaurantid")
@GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "restaurant_seq_gen")
@SequenceGenerator(name = "restaurant_seq_gen",sequenceName = "restaurant_seq")
private int restaurantId;

@Column(name = "restaurantname")
private String restaurantName;

@Column(name = "restaurantemail")
private String restaurantEmail;

@Column(name = "restaurantaddress")
private String restaurantAddress;

@Column(name = "password")
private String password;

@Column(name = "postleitzahl")
private int postLeitZahl;

@Column(name = "phonenumber")
private int phoneNumber;

@Column(name = "speciality")
private String restaurantSpeciality;

@Column(name = "city")
private String city;


@Transient
private List<GrantedAuthority> authorities;

@OneToMany(mappedBy = "restaurantforrating", fetch = FetchType.EAGER, cascade = CascadeType.REMOVE)
private Set<Rating> ratingSet = new HashSet<>();

public Set<Rating> getRatingSet(){return this.ratingSet;}

public void setRatingSet(Set<Rating> ratingSet){
    this.ratingSet = ratingSet;
}



@OneToMany(mappedBy = "restaurantmenucard", fetch = FetchType.EAGER, cascade = CascadeType.REMOVE)
private Set<MenuCard> menuCardSet = new HashSet<>();

public Set<MenuCard> getMenuCardSet(){
    return this.menuCardSet;
}

public void setMenuCardSet(Set<MenuCard> menuCardSet){
    this.menuCardSet = menuCardSet;
}

Android code to send object :

String url = "http://192.168.178.40:8080/restaurant/add";

@Override
public void addRestaurant(Restaurant restaurant) {
    Log.d("Restaurant Name",restaurant.getRestaurantName());
    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            RestTemplate restTemplate = new RestTemplate();
            restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
            restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
           // HttpHeaders headers = new HttpHeaders();
           // headers.setContentType(MediaType.APPLICATION_JSON);
           // HttpEntity entity = new HttpEntity(restaurant,headers);
            //ResponseEntity<String> out = restTemplate.exchange(url, HttpMethod.POST,entity,String.class);
            String response = restTemplate.postForObject(url,restaurant,String.class);
            Log.d(response,"Response from webserver is");
        }
    });
    thread.setPriority(Process.THREAD_PRIORITY_BACKGROUND);
    thread.start();
}

Android error log :

02-19 10:01:14.099    2266-2285/com.example.myapp E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-166
    Process: com.example.myapp, PID: 2266
    org.springframework.web.client.HttpClientErrorException: 400 Bad Request
            at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:76)
            at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:524)
            at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:481)
            at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:439)
            at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:317)
            at com.example.myapp.RestaurantServiceImpl$1.run(RestaurantServiceImpl.java:32)
            at java.lang.Thread.run(Thread.java:818)
02-19 10:01:14.826    2266-2266/com.example.myapp I/Choreographer﹕ Skipped 41 frames!  The application may be doing too much work on its main thread.

THis is the current situation guys. It is really getting annoying now as I don't know and cannot find on net what I am doing wrong. Does the Java class in server need to completely match the one on Android, as there are no mappings in Android Java class.


Solution

  • Try adding @RequestBody annotation to your spring controller.

    @RequestMapping(value = "/restaurant/add",method = RequestMethod.POST)
        @ResponseBody
        public String addRestaurantWebView(@RequestBody Restaurant restaurant){
            System.out.println(restaurant.getRestaurantName());
            this.restaurantService.addRestaurant(restaurant);
           return null;
        }