복붙노트

[SPRING] Spring : POST body에서 매개 변수를 얻는 방법?

SPRING

Spring : POST body에서 매개 변수를 얻는 방법?

내 게시물 요청 본문에서 매개 변수를 가져와야하는 봄을 사용하는 웹 서비스? 신체의 내용은 다음과 같습니다 : -

source=”mysource”

&json=
{
    "items": [
        {
            "username": "test1",
            "allowed": true
        },
        {
            "username": "test2",
            "allowed": false
        }
    ]
}

그리고 웹 서비스 방법은 다음과 같습니다.

@RequestMapping(value = "/saveData", headers="Content-Type=application/json", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<Boolean> saveData(@RequestBody String a) throws MyException {
        return new ResponseEntity<Boolean>(uiRequestProcessor.saveData(a),HttpStatus.OK);

    }

몸에서 매개 변수를 가져 오는 방법을 알려주세요. 나는 몸 전체를 나의 끈으로 얻을 수는 있지만 그것이 유효한 접근이라고 생각하지 않는다. 어떻게 더 진행해야하는지 알려주세요.

해결법

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

    1.param을 요청할 수 있습니다.

    param을 요청할 수 있습니다.

    @ResponseBody
    public ResponseEntity<Boolean> saveData(HttpServletRequest request,
                HttpServletResponse response, Model model){
       String jsonString = request.getParameter("json");
    }
    
  2. ==============================

    2.게시물의 전체 본문을 POJO로 가져올 수 있습니다. 다음은 비슷한 것입니다.

    게시물의 전체 본문을 POJO로 가져올 수 있습니다. 다음은 비슷한 것입니다.

    @RequestMapping(value = { "/api/pojo/edit" }, method = RequestMethod.POST, produces = "application/json", consumes = "application/json")
    @ResponseBody
    public Boolean editWinner( @RequestBody Pojo pojo) { 
    

    Pojo의 각 필드 (getter / setters 포함)는 컨트롤러가받는 Json 요청 객체와 일치해야합니다.

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

    3.MappingJacksonHttpMessageConverter를 사용하여 json을 POJO에 바인딩 할 수 있습니다. 따라서 컨트롤러 서명을 읽을 수 있습니다 : -

    MappingJacksonHttpMessageConverter를 사용하여 json을 POJO에 바인딩 할 수 있습니다. 따라서 컨트롤러 서명을 읽을 수 있습니다 : -

      public ResponseEntity<Boolean> saveData(@RequestBody RequestDTO req) 
    

    RequestDTO는 jackson serialize / deserializing으로 작업 할 수 있도록 적절하게 주석 처리 된 bean이어야합니다. * -servlet.xml 파일에는 다음과 같이 RequestMappingHandler에 등록 된 Jackson 메시지 변환기가 있어야합니다.

      <list >
        <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
    
      </list>
    </property>
    </bean>
    
  4. ==============================

    4.수업 시간에 이렇게하세요.

    수업 시간에 이렇게하세요.

    @RequestMapping(value = "/saveData", method = RequestMethod.POST)
     @ResponseBody
        public ResponseEntity<Boolean> saveData(HttpServletResponse response,Bean beanName) throws MyException {
            return new ResponseEntity<Boolean>(uiRequestProcessor.saveData(a),HttpStatus.OK);
    
    }
    

    페이지에서 다음과 같이하십시오.

    <form enctype="multipart/form-data" action="<%=request.getContextPath()%>/saveData" method="post" name="saveForm" id="saveForm">
    <input type="text" value="${beanName.userName }" id="username" name="userName" />
    
    </from>
    
  5. ==============================

    5.이 수입품이 필요합니다 ...

    이 수입품이 필요합니다 ...

    import javax.servlet.*;
    import javax.servlet.http.*;
    

    그리고 Maven을 사용한다면 프로젝트의 기본 디렉토리에있는 pom.xml 파일의 dependencies 블록에이 파일이 필요합니다.

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <scope>provided</scope>
    </dependency>
    

    그런 다음 Jason의 위에 나열된 수정 프로그램이 작동합니다.

    @ResponseBody
        public ResponseEntity<Boolean> saveData(HttpServletRequest request,
            HttpServletResponse response, Model model){
            String jsonString = request.getParameter("json");
        }
    
  6. ==============================

    6.@RequestBodyParam을 사용해 볼 수 있습니다.

    @RequestBodyParam을 사용해 볼 수 있습니다.

    @RequestMapping(value = "/saveData", headers="Content-Type=application/json", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<Boolean> saveData(@RequestBodyParam String source,@RequestBodyParam JsonDto json) throws MyException {
        ...
    }
    

    https://github.com/LambdaExpression/RequestBodyParam

  7. from https://stackoverflow.com/questions/22163397/spring-how-to-get-parameters-from-post-body by cc-by-sa and MIT license