gpt4 book ai didi

rest - 如何在 Heroku 上使用 JAX-RS 找出传入的 RESTful 请求的 IP?

转载 作者:行者123 更新时间:2023-12-03 09:01:38 27 4
gpt4 key购买 nike

我正在编写基于示例托管在 Heroku 上的 Java RESTful 服务 -> https://api.heroku.com/myapps/template-java-jaxrs/clone

我的示例服务是:

package com.example.services;

import com.example.models.Time;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/time")
@Produces(MediaType.APPLICATION_JSON)
public class TimeService {

@GET
public Time get() {
return new Time();
}

}

我的主要是:
public class Main {

public static final String BASE_URI = getBaseURI();

/**
* @param args
*/
public static void main(String[] args) throws Exception{
final Map<String, String> initParams = new HashMap<String, String>();
initParams.put("com.sun.jersey.config.property.packages","services.contracts");

System.out.println("Starting grizzly...");
SelectorThread threadSelector = GrizzlyWebContainerFactory.create(BASE_URI, initParams);
System.out.println(String.format("Jersey started with WADL available at %sapplication.wadl.",BASE_URI, BASE_URI));
}

private static String getBaseURI()
{
return "http://localhost:"+(System.getenv("PORT")!=null?System.getenv("PORT"):"9998")+"/";
}

}

我的问题是如何在我的服务中找到请求来自的 IP 地址和端口组合?我在@Context 上阅读了注入(inject) javax.ws.rs.core.HttpHeaders、javax.ws.rs.core.Request 等的内容。但是,不存在传入的 IP 或端口信息。

我知道如果您实现 com.sun.grizzly.tcp.Adapter,您可以执行以下操作:
public static void main(String[] args) {
SelectorThread st = new SelectorThread();
st.setPort(8282);
st.setAdapter(new EmbeddedServer());
try {
st.initEndpoint();
st.startEndpoint();
} catch (Exception e) {
System.out.println("Exception in SelectorThread: " + e);
} finally {
if (st.isRunning()) {
st.stopEndpoint();
}
}
}

public void service(Request request, Response response)
throws Exception {
String requestURI = request.requestURI().toString();

System.out.println("New incoming request with URI: " + requestURI);
System.out.println("Request Method is: " + request.method());

if (request.method().toString().equalsIgnoreCase("GET")) {
response.setStatus(HttpURLConnection.HTTP_OK);
byte[] bytes = "Here is my response text".getBytes();

ByteChunk chunk = new ByteChunk();
response.setContentLength(bytes.length);
response.setContentType("text/plain");
chunk.append(bytes, 0, bytes.length);
OutputBuffer buffer = response.getOutputBuffer();
buffer.doWrite(chunk, response);
response.finish();
}
}

public void afterService(Request request, Response response)
throws Exception {
request.recycle();
response.recycle();
}

和访问
    request.remoteAddr()

但我真的很想以一种更结构化的方式分离我的 RESTful API,就像在我的第一个实现中一样。

任何帮助将不胜感激。谢谢!

最佳答案

可以注入(inject) HttpServletRequest :

@GET
@Produces(MediaType.TEXT_PLAIN)
public Response getIp(@Context HttpServletRequest req) {
String remoteHost = req.getRemoteHost();
String remoteAddr = req.getRemoteAddr();
int remotePort = req.getRemotePort();
String msg = remoteHost + " (" + remoteAddr + ":" + remotePort + ")";
return Response.ok(msg).build();
}

关于rest - 如何在 Heroku 上使用 JAX-RS 找出传入的 RESTful 请求的 IP?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13171265/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com