PUT方法的ModelAttribute未填充值

PUT方法的ModelAttribute未填充值

本文介绍了Restful PUT方法的ModelAttribute未填充值(JSON)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring MVC构建一个完全宁静的Web应用程序.当我有一个PUT方法时,不会填充我的@ModelAttribute表单bean(所有值都为null).如果我使用POST方法,则所有内容都会正确填充.

I'm building a fully restful web app using Spring MVC. When I have a PUT method, my @ModelAttribute form bean is not populated (all values null). If I use the POST method, everything populates correctly.

我向邮递员查询( https://chrome.google .com/webstore/detail/postman-rest-client/fdmmgilgnpjigdojojpjoooidkmcomcm )图片要求邮递员: http://www.hostingpics.net/viewer.php?id = 474577probleme.jpg

I do a query with Postman (https://chrome.google.com/webstore/detail/postman-rest-client/fdmmgilgnpjigdojojpjoooidkmcomcm)Image Requete Postman : http://www.hostingpics.net/viewer.php?id=474577probleme.jpg

@Entity
@Table(name = "positiongps")
public class PositionGPS implements BaseEntity<Long> {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", nullable = false, columnDefinition = "SERIAL", updatable = false)
private Long id;

@Column(name = "latitude", precision = 11, scale = 7, columnDefinition = "NUMERIC", nullable = false, updatable = true, unique = false)
private BigDecimal latitude;

@Column(name = "longitude", precision = 11, scale = 7, columnDefinition = "NUMERIC", nullable = false, updatable = true, unique = false)
private BigDecimal longitude;

// ** Constructeur **//

public PositionGPS() {
    super();
    latitude = new BigDecimal("0");
    longitude = new BigDecimal("0");
}

// ** Get and Set **//

.

@ResponseBody
@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
public boolean update(@ModelAttribute("positionGPS") PositionGPS positionGPS, @PathVariable Long id, Model model) {
    LOG.debug("update :: IN, PositionGPS.Id=[" + id + "]");
    PositionGPS positionGPSOld = positionGPSService.getById(id);
    LOG.debug("update :: getId=[" + positionGPS.getId() + "]");
    LOG.debug("update :: getLatitude=[" + positionGPS.getLatitude() + "]");
    LOG.debug("update :: getLongitude=[" + positionGPS.getLongitude() + "]");

    try {
        if (positionGPSOld != null) {
            positionGPSOld.setLatitude(positionGPS.getLatitude());
            positionGPSOld.setLongitude(positionGPS.getLongitude());
            PositionGPS newpositionGPS = positionGPSService.update(positionGPSOld);
        } else {
            LOG.debug("update :: PositionGPS Error test");
        }

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return true;
}

web.xml

<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<filter>
    <filter-name>httpMethodFilter</filter-name>
    <filter-  class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>httpMethodFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

我的控制台:

DEBUG: PositionGPSController - update :: IN,   PositionGPS.Id=[136]
DEBUG: PositionGPSController - update :: getId=[136]
DEBUG: PositionGPSController - update :: getLatitude=[0]
DEBUG: PositionGPSController - update :: getLongitude=[0]

推荐答案

我要替换

@ModelAttribute("positionGPS") PositionGPS positionGPS, @PathVariable Long id, Model model

@RequestBody PositionGPS positionGPS, @PathVariable Long id, Model model)

链接帮助: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-config-enable

Spring MVC:请勿反序列化JSON请求正文

这篇关于Restful PUT方法的ModelAttribute未填充值(JSON)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 10:33