2
비동기 지원 (대기에서 제공)으로 REST 서비스에서 POST 요청을하려고합니다. 여기처럼 내 서비스가 어떻게 표시되는지를 보여줍니다 : 나는 asyncHttpClient를 사용하여 POST 요청을 할 노력하고있어asynchttpclient가있는 JSON 게시물
package co.damt.services;
import javax.ws.rs.Consumes;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Produces;
import org.atmosphere.annotation.Broadcast;
import org.atmosphere.annotation.Suspend;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* REST Web Service
*
* @author damt
*/
@Path("/")
public class GenericResource {
private final Logger logger = LoggerFactory.getLogger(GenericResource.class);
@Suspend(contentType = "application/json")
@GET
public String suspend()
{
return "";
}
@Broadcast(writeEntity = false)
@POST
@Produces("application/json")
public Message broadcast(Message message)
{
logger.info(message.getAuthor()+"@"+message.getTime()+": "+message.getMessage());
return message;
}
}
나는 200의 상태를 얻을 수 있지만, 메시지가 수신 된 서비스로부터 로그 없다 :
private final static ObjectMapper mapper = new ObjectMapper();
private static Request request;
public static void main(String[] args)
{
AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
String jsonContent="";
try {
jsonContent = mapper.writeValueAsString(new Message("diego", "Hello World"));
System.out.println(jsonContent);
} catch (IOException ex) {
Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
}
request = asyncHttpClient.preparePost("http://localhost:8080/Test/").
setHeader("Content-Type","application/json").
setHeader("Content-Length", ""+jsonContent.length()).
setBody(jsonContent).
build();
ListenableFuture<Integer> f= null;
try {
f = asyncHttpClient.executeRequest(request,
new AsyncCompletionHandler<Integer>(){
@Override
public Integer onCompleted(Response response) throws Exception{
// Do something with the Response
System.out.println(response.getStatusCode());
return response.getStatusCode();
}
@Override
public void onThrowable(Throwable t){
// Something wrong happened.
}
});
} catch (IOException ex) {
Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
}
int statusCode;
if (f!=null)
{
try {
statusCode = f.get();
} catch (InterruptedException ex) {
Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
} catch (ExecutionException ex) {
Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
}
}
asyncHttpClient.close();
나는 메시지를받은 Apache HttpClient 서비스 로그를 사용하고 200 상태 코드 얻을 때 :
@SuppressWarnings("deprecation")
final HttpClient httpClient = new DefaultHttpClient();
@SuppressWarnings("deprecation")
StringEntity requestEntity= null;
try {
requestEntity = new StringEntity(
mapper.writeValueAsString(new Message("diego", "Hello World!")),
ContentType.APPLICATION_JSON);
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
} catch(IOException ex) {
Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
}
HttpPost postMethod = new HttpPost("http://localhost:8080/Test/chat/");
postMethod.setEntity(requestEntity);
HttpResponse statusCode= null;
try {
statusCode = httpClient.execute(postMethod);
} catch (IOException ex) {
Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
}
postMethod.releaseConnection();
if (statusCode != null)
{
System.out.println(statusCode.toString());
}
왜 asyncHttpClient가 작동하지 않는?
JDK 1.6과 함께 AHC 버전 1.7.13을 사용하고 있습니다. 공급자를 변경하지 않았으므로 기본값을 사용하고있는 것 같습니다. –
Apache HttpClient가 http : // localhost : 8080/Test/chat /에 게시하는 동안 실제로 http : // localhost : 8080/Test /에 AHC 샘플 게시물을 보내시겠습니까? –
미안하다. 고맙습니다, 고맙습니다! –