【问题标题】:RestFul With Spring POST MethodRestFul 使用 Spring POST 方法
【发布时间】:2012-12-24 23:34:31
【问题描述】:

我一直在开发一个从 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);

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

【问题讨论】:

  • 你知道实际发送的是什么吗?
  • 我正在发送该预订对象。我在做什么有问题吗?我使用 XML 顺便说一句
  • 嗯,单看代码很难判断,首先要知道数据序列化是否正确。
  • 我真的不知道,但它会在我的数据库中插入 Null 记录,因为我将 Hibernate Cascade 设置为 All。由于请求正文为空,因此记录为空。

标签: android spring hibernate rest


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-07
    • 1970-01-01
    • 1970-01-01
    • 2017-04-06
    • 1970-01-01
    • 2013-06-19
    • 1970-01-01
    • 2016-11-22
    相关资源
    最近更新 更多