복붙노트

[SPRING] PUT 메소드가 채워지지 않은 값을위한 ModelAttribute (JSON)

SPRING

PUT 메소드가 채워지지 않은 값을위한 ModelAttribute (JSON)

Spring MVC를 사용하여 완전히 안정된 웹 앱을 만들고 있습니다. PUT 메서드가있을 때 @ModelAttribute 폼 bean이 채워지지 않습니다 (모든 값은 null입니다). POST 메서드를 사용하면 모든 것이 올바르게 채워집니다.

Postman (https://chrome.google.com/webstore/detail/postman-rest-client/fdmmgilgnpjigdojojpjoooidkmcomcm)에 대한 질문을합니다. 이미지 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;
}

을 포함한다.

<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]

해결법

  1. ==============================

    1.내가 대체

    내가 대체

    @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 요청 본문을 deserialize하지 않는다.

  2. ==============================

    2.HTML 양식을 사용하는 경우 PUT을 지원하지 않습니다. PUT을 지원하려면 Ajax로 직렬화 된 양식의 내용을 보내야합니다. XMLHttpRequest를 사용하면 모든 주요 동사 (OPTIONS, GET, POST, PUT, DELETE)를 사용할 수 있습니다. Ajax를 사용할 수 없다면 POST만으로 모든 것을 터널링하여 생성, 업데이트 및 삭제 작업을 수행 할 수 있습니다.

    HTML 양식을 사용하는 경우 PUT을 지원하지 않습니다. PUT을 지원하려면 Ajax로 직렬화 된 양식의 내용을 보내야합니다. XMLHttpRequest를 사용하면 모든 주요 동사 (OPTIONS, GET, POST, PUT, DELETE)를 사용할 수 있습니다. Ajax를 사용할 수 없다면 POST만으로 모든 것을 터널링하여 생성, 업데이트 및 삭제 작업을 수행 할 수 있습니다.

  3. ==============================

    3.WebApplicationInitializer 클래스에 아래 필터를 추가하거나 web.xml에 해당 xml 코드를 추가하십시오.

    WebApplicationInitializer 클래스에 아래 필터를 추가하거나 web.xml에 해당 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, "/*");
    
  4. from https://stackoverflow.com/questions/19600532/modelattribute-for-restful-put-method-not-populated-value-json by cc-by-sa and MIT license