get value from http post request

 

We all know that the parameter value submitted by the get method can be obtained from the http request through request.getParameter("someParam"), then what about the body (such as xml, json, etc.) in the post request?

private String getRequestBody(final HttpServletRequest request) {
		
		String charset = request.getCharacterEncoding();   
		 if (charset == null) {   
		     charset = DEFAULT_ENCODE;   
		 }  
		 String str, wholeStr;
		try {
			BufferedReader br = request.getReader();
			 wholeStr = "";
			 while((str = br.readLine()) != null){
			 wholeStr += str;
			 }
			 return wholeStr;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace ();
		}
}

 

 

    String type in XML format and java object conversion class

import java.io.StringReader;  
import java.io.StringWriter;  
import java.util.Collection;  
  
import javax.xml.bind.JAXBContext;  
import javax.xml.bind.JAXBElement;  
import javax.xml.bind.JAXBException;  
import javax.xml.bind.Marshaller;  
import javax.xml.bind.Unmarshaller;  
import javax.xml.bind.annotation.XmlAnyElement;  
import javax.xml.namespace.QName;  
  
import org.apache.commons.lang.StringUtils;  
  
/**
 * Use Jaxb2.0 to implement Binder of XML<->Java Object.
 *  
 * Special support for the case where the Root object is a List.
 *  
 * @author
 */  
public class JaxbUtil {  
    // Multithread safe Context.  
    private JAXBContext jaxbContext;  
  
    /**
     * @param types
     * All types of Root objects that need to be serialized.
     */  
    public JaxbUtil(Class<?>... types) {  
        try {  
            jaxbContext = JAXBContext.newInstance(types);  
        } catch (JAXBException e) {  
            throw new RuntimeException(e);  
        }  
    }  
  
    /**
     * Java Object->Xml.
     */  
    public String toXml(Object root, String encoding) {  
        try {  
            StringWriter writer = new StringWriter();  
            createMarshaller(encoding).marshal(root, writer);  
            return writer.toString();  
        } catch (JAXBException e) {  
            throw new RuntimeException(e);  
        }  
    }  
  
    /**
     * Java Object->Xml, especially supports the case where the Root Element is a Collection.
     */  
    @SuppressWarnings("unchecked")  
    public String toXml(Collection root, String rootName, String encoding) {  
        try {  
            CollectionWrapper wrapper = new CollectionWrapper();  
            wrapper.collection = root;  
  
            JAXBElement<CollectionWrapper> wrapperElement = new JAXBElement<CollectionWrapper>(  
                    new QName(rootName), CollectionWrapper.class, wrapper);  
  
            StringWriter writer = new StringWriter();  
            createMarshaller(encoding).marshal(wrapperElement, writer);  
  
            return writer.toString();  
        } catch (JAXBException e) {  
            throw new RuntimeException(e);  
        }  
    }  
  
    /**
     * Xml->Java Object.
     */  
    @SuppressWarnings("unchecked")  
    public <T> T fromXml(String xml) {  
        try {  
            StringReader reader = new StringReader(xml);  
            return (T) createUnmarshaller().unmarshal(reader);  
        } catch (JAXBException e) {  
            throw new RuntimeException(e);  
        }  
    }  
  
    /**
     * Xml->Java Object, support case sensitive or insensitive.
     */  
    @SuppressWarnings("unchecked")  
    public <T> T fromXml(String xml, boolean caseSensitive) {  
        try {  
            String fromXml = xml;  
            if (!caseSensitive)  
                fromXml = xml.toLowerCase();  
            StringReader reader = new StringReader(fromXml);  
            return (T) createUnmarshaller().unmarshal(reader);  
        } catch (JAXBException e) {  
            throw new RuntimeException(e);  
        }  
    }  
  
    /**
     * Create Marshaller, set encoding (can be Null).
     */  
    public Marshaller createMarshaller(String encoding) {  
        try {  
            Marshaller marshaller = jaxbContext.createMarshaller();  
  
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);  
  
            if (StringUtils.isNotBlank(encoding)) {  
                marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);  
            }  
            return marshaller;  
        } catch (JAXBException e) {  
            throw new RuntimeException(e);  
        }  
    }  
  
    /**
     * Create UnMarshaller.
     */  
    public Unmarshaller createUnmarshaller() {  
        try {  
            return jaxbContext.createUnmarshaller();  
        } catch (JAXBException e) {  
            throw new RuntimeException(e);  
        }  
    }  
  
    /**
     * Encapsulates the case where the Root Element is a Collection.
     */  
    public static class CollectionWrapper {  
        @SuppressWarnings("unchecked")  
        @XmlAnyElement  
        protected Collection collection;  
    }  
}

 

 

@Override
	public AuibInsuranceCallback formatCallBackData(String source, String sign) {
		if(this.checkCallbackInfo(source, sign)){
			JaxbUtil resultBinder = new JaxbUtil(AuibInsuranceCallback.class);  
			AuibInsuranceCallback auibInsuranceCallback = resultBinder.fromXml(source);
			return auibInsuranceCallback;
		}else{
			return null;
		}
		
	}

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326780275&siteId=291194637