Restful PUT方法的ModelAttribute没有填充值(JSON)

时间:2013-10-25 22:45:37

标签: java json spring rest put

我正在使用Spring MVC构建一个完全安静的Web应用程序。当我有一个PUT方法时,我的@ModelAttribute表单bean没有被填充(所有值都为null)。如果我使用POST方法,一切都正确填充。

我使用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]

3 个答案:

答案 0 :(得分:2)

我替换

@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: Don't deserialize JSON request body

答案 1 :(得分:0)

如果您使用的是HTML表单,则不支持PUT。如果要支持PUT,则需要发送使用Ajax序列化的表单的内容。使用XMLHttpRequest,您可以使用所有主要动词(OPTIONS,GET,POST,PUT,DELETE)。如果您不能使用Ajax,则唯一的其他选择是通过POST将所有内容通过POST进行创建,更新和删除操作。

答案 2 :(得分:0)

将以下过滤器添加到WebApplicationInitializer类或相应的xml代码到web.xml

final FilterRegistration.Dynamic httpMethodFilter = servletContext.addFilter("hiddenHttpMethodFilter", new HiddenHttpMethodFilter());
        httpMethodFilter.addMappingForUrlPatterns(null, true, "/*");

        final FilterRegistration.Dynamic putFormFilter = servletContext.addFilter("httpPutFormContentFilter", new HttpPutFormContentFilter());
        putFormFilter.addMappingForUrlPatterns(null, true, "/*");