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/addition")
public ModelAndView addRestaurant(@ModelAttribute("restaurant") Restaurant restaurant, ModelAndView modelAndView){
modelAndView.addObject(new Restaurant());
this.restaurantService.addRestaurant(restaurant);
return modelAndView;
}
//Default Controller I use for browsers
@RequestMapping(value = "/restaurant/add")
public String addRestaurantWebView(@ModelAttribute("restaurant") Restaurant restaurant, Model model){
model.addAttribute("restaurant", new Restaurant());
this.restaurantService.addRestaurant(restaurant);
this.restaurantService.listRestaurants();
return "redirect:/";
}
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;
// Getters and setters ommitted
}
Android code to send object :
String url = "http://ift.tt/1AlfdxA";
@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());
String response = restTemplate.postForObject(url,restaurant,String.class);
Log.d(response,"Response from webserver is");
}
});
thread.start();
}
I have never worked in RESTful services. Any pointers would be nice. Thanks a lot.
Aucun commentaire:
Enregistrer un commentaire