使用Spring POST方法的RestFul

时间:2012-12-24 23:34:32

标签: android spring hibernate rest

我一直在研究从Android设备上消耗的Spring RESTful应用程序。我正在尝试发送POST请求时出现问题。我的对象在服务器端收到,但所有属性都为空。

我的控制器:

@Controller
public class BeanController {

@Autowired
private ReservationService reservationService;

    @RequestMapping(value = "reservation/add/{dateR}", method = RequestMethod.POST)
public ModelAndView addReservation(@RequestBody Reservation reservation, @PathVariable String dateR) {
    Date date = Date.valueOf(dateR);
    boolean result = reservationService.saveReservation(reservation,date);
    return new ModelAndView("beanXmlView", BindingResult.MODEL_KEY_PREFIX + "reservation", result);
    }
}

在我的客户端,我确保设置了对象属性。

我的客户方:

Reservation reservation = new Reservation();
            reservation.setDateReservation(Model.date);
            reservation.setSalle(Model.salle);
            if(Model.selectedEquipements.size() == 0)
                reservation.setEquipements(new ArrayList<Equipement>());
            else reservation.setEquipements(Model.selectedEquipements);
            reservation.setPlagesHoraires(Model.selectedPlageHoraires);
            reservation.setUtilisateur(user);
            final String url = "http://10.0.2.2:8080/HibernateSpringREST/";
            RestTemplate restTemplate = new RestTemplate();
            // Add the Jackson and String message converters
            restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
            restTemplate.getMessageConverters().add(new SimpleXmlHttpMessageConverter());
            // Make the HTTP POST request, marshaling the request to JSON, and the response to a String
            restTemplate.postForObject(url + "reservation/add/"+Model.date, reservation, Reservation.class);

是否有人知道可能导致此问题的原因?

1 个答案:

答案 0 :(得分:0)

根据给出的信息,我无法推断出问题所在。为了找到答案,我将按照以下步骤进行:

  1. 以调试模式运行服务器,并在addReservation
  2. 的第一行设置断点
  3. 观察reservation参数是否符合预期
  4. 如果没有,请检查从客户端发送的POST请求,on the device or with Fiddler/Wireshark
相关问题