1

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.

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>

My console :

DEBUG: PositionGPSController - update :: IN,   PositionGPS.Id=[136]
DEBUG: PositionGPSController - update :: getId=[136]
DEBUG: PositionGPSController - update :: getLatitude=[0]
DEBUG: PositionGPSController - update :: getLongitude=[0]
MaximeF
  • 4,913
  • 4
  • 37
  • 51

3 Answers3

2

Im replace

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

for

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

Link help: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-config-enable

and

Spring MVC: Don't deserialize JSON request body

Community
  • 1
  • 1
MaximeF
  • 4,913
  • 4
  • 37
  • 51
  • And did that work? If it did then I assume you already have Jackson on the classpath for marshaling from JSON to an object. You specified you're using Postman, but that obviously isn't going to be your final working solution. You'll have to use either Ajax or a form post. If you're using a form post this still isn't going to work because that doesn't support a PUT operation cross-browser. –  Oct 26 '13 at 15:52
  • I tested on a cell application and backbone and everything works. – MaximeF Oct 26 '13 at 15:55
0

If you're using an HTML form it doesn't support PUT. If you want to support PUT then you need to send the contents of the form serialized with Ajax. With XMLHttpRequest you can use all the major verbs (OPTIONS, GET, POST, PUT, DELETE). If you can't use Ajax the only other option is to tunnel everything through POST for create, update and delete operations.

0

Add the below filter to your WebApplicationInitializer class or corresponding xml code to 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, "/*");
Arvind
  • 199
  • 1
  • 5