2016-04-12 53 views
0

我使用Spring的AbstractHttpMessageConverter來實例化我自己的對象。在Spring中獲取HTTP請求參數HTTPMessageConverter

轉換

public class PaypalIPNHttpMessageConverter extends AbstractHttpMessageConverter<IPNMessage> { 

    public PaypalIPNHttpMessageConverter() { 
     super(MediaType.APPLICATION_FORM_URLENCODED, MediaType.TEXT_PLAIN); 
    } 

    @Override 
    protected boolean supports(Class<?> clazz) { 
     return clazz == IPNMessage.class; 
    } 

    @Override 
    protected IPNMessage readInternal(Class<? extends IPNMessage> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { 
     //Converts HTTPRequest into map<string,string> that IPNMessage can then parse 
     String requestString = IOUtils.toString(inputMessage.getBody(), "UTF-8"); 
     Map<String, String[]> requestMap = new LinkedHashMap<>(); 
     for (String keyValue : requestString.split("&")) { //each key value is delimited by & 
      String[] pairs = keyValue.split("=", 2); // = pairs a key to a value 
      requestMap.put(pairs[0], pairs[1].split(",")); // , splits multiple values for that key 
     } 
     return new IPNMessage(requestMap); 
    } 

    @Override 
    protected void writeInternal(IPNMessage ipnMessage, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { 

    } 
} 

在readINternal(),我通過了HttpInputMessage對象,它僅具有產生HttpRequest中的一個InputStream getBody()函數。

我試圖編寫自己的代碼來解析和構建ParameterMap,但如果urlencoding不同,它並不總是有效。

反正我有可以從轉換器獲得Spring的的WebRequestHttpServletRequest的對象,並使用有精彩getParameterMap()函數?

TL; DR

反正是有使用的WebRequest或HttpServletRequest的中的MessageConverter代替HttpInput這樣我就可以用精彩getParameterMap()函數,而不是重新發明輪子?

感謝

+0

您可以通常只是自動裝配HttpServletRequest的春天管理豆類(如只添加這是一個領域:'@Autowired HttpServletRequest的請求;')。請注意,Spring框架並不打算在消息轉換器中訪問url參數。可能有更好的地方插入你的功能(轉換服務?參數解析器?) –

回答

2

看看它是如何在春季FormHttpMessageConverter實現。它就像您的解決方案一樣使用&來標記請求參數。但是,如果提供的話,它會確定從Content-Type請求標頭中使用的字符集。否則,使用默認的UTF-8

//extract from FormHttpMessageConverter 
public MultiValueMap<String, String> read(Class<? extends MultiValueMap<String, ?>> clazz, 
      HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { 

     MediaType contentType = inputMessage.getHeaders().getContentType(); 
     Charset charset = (contentType.getCharSet() != null ? contentType.getCharSet() : this.charset); 
     String body = StreamUtils.copyToString(inputMessage.getBody(), charset); 

     String[] pairs = StringUtils.tokenizeToStringArray(body, "&"); 
     MultiValueMap<String, String> result = new LinkedMultiValueMap<String, String>(pairs.length); 
     for (String pair : pairs) { 
      int idx = pair.indexOf('='); 
      if (idx == -1) { 
       result.add(URLDecoder.decode(pair, charset.name()), null); 
      } 
      else { 
       String name = URLDecoder.decode(pair.substring(0, idx), charset.name()); 
       String value = URLDecoder.decode(pair.substring(idx + 1), charset.name()); 
       result.add(name, value); 
      } 
     } 
     return result; 
    }