Как стать сытым, REST запрашивает тело с помощью Джерси?

Или запишите свои настройки в xml файле и сохраните его с помощью Изолированное устройство хранения данных . В зависимости от хранилища Вы используете его, сохраняет его в папке Application Data. Можно также выбрать, роуминг включил хранилище, что означает, когда пользователь входит в систему различный компьютер перемещение настроек с ними.

60
задан Simulant 13 October 2015 в 16:12
поделиться

2 ответа

Оказывается, вам совсем не нужно много делать.

См. Ниже - параметр x будет содержать полное тело HTTP (в нашем случае это XML).

@POST
public Response go(String x) throws IOException {
    ...
}
83
ответ дан 24 November 2019 в 17:42
поделиться

It does seem you would have to use a MessageBodyReader here. Here's an example, using jdom:

import org.jdom.Document;
import javax.ws.rs.ext.MessageBodyReader;
import javax.ws.rs.ext.Provider;
import javax.ws.rs.ext.MediaType;
import javax.ws.rs.ext.MultivaluedMap;
import java.lang.reflect.Type;
import java.lang.annotation.Annotation;
import java.io.InputStream;

@Provider // this annotation is necessary!
@ConsumeMime("application/xml") // this is a hint to the system to only consume xml mime types
public class XMLMessageBodyReader implements MessageBodyReader<Document> {
  private SAXBuilder builder = new SAXBuilder();

  public boolean isReadable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType) {
    // check if we're requesting a jdom Document
    return Document.class.isAssignableFrom(type);
  }

  public Document readFrom(Class type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) {
    try {
      return builder.build(entityStream);
    }
    catch (Exception e) {
      // handle error somehow
    }
  } 
}

Add this class to the list of resources your jersey deployment will process (usually configured via web.xml, I think). You can then use this reader in one of your regular resource classes like this:

@Path("/somepath") @POST
public void handleXMLData(Document doc) {
  // do something with the document
}

I haven't verified that this works exactly as typed, but that's the gist of it. More reading here:

0
ответ дан 24 November 2019 в 17:42
поделиться
Другие вопросы по тегам:

Похожие вопросы: