相关疑难解决方法(0)

HttpServletRequest中getRequestURI和getPathInfo方法之间的区别是什么?

我正在制作一个简单,非常轻巧的前置控制器.我需要将请求路径与不同的处理程序(操作)匹配,以便选择正确的路径.

在我的本地计算机上HttpServletRequest.getPathInfo()HttpServletRequest.getRequestURI()返回相同的结果.但我不确定它们会在生产环境中返回什么.

那么,这些方法和我应该选择什么之间的区别是什么?

java servlets

133
推荐指数
4
解决办法
15万
查看次数

我应该如何在RESTful JAX-RS Web服务中记录未捕获的异常?

我使用Jersey和Jackson在Glassfish 3.1.2下运行RESTful Web服务:

@Stateless
@LocalBean
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("users")
public class UserRestService {
    private static final Logger log = ...;

    @GET
    @Path("{userId:[0-9]+}")
    public User getUser(@PathParam("userId") Long userId) {
        User user;

        user = loadUserByIdAndThrowApplicableWebApplicationExceptionIfNotFound(userId);

        return user;
    }
}
Run Code Online (Sandbox Code Playgroud)

对于预期的异常,我抛出适当的WebApplicationException,我对发生意外异常时返回的HTTP 500状态感到满意.

我现在想为这些意外的异常添加日志记录,但是尽管搜索,但是无法找到我应该如何处理这个问题.

没有结果的尝试

我已经尝试使用a Thread.UncaughtExceptionHandler并且可以确认它是在方法体内应用的,但是它的uncaughtException方法永远不会被调用,因为其他东西在它们到达我的处理程序之前处理未捕获的异常.

其他想法:#1

我见过一些人使用的另一个选项是ExceptionMapper,它捕获所有异常,然后过滤掉WebApplicationExceptions:

@Provider
public class ExampleExceptionMapper implements ExceptionMapper<Throwable> {
    private static final Logger log = ...;

    public Response toResponse(Throwable t) {
        if (t instanceof WebApplicationException) {
            return ((WebApplicationException)t).getResponse();
        } …
Run Code Online (Sandbox Code Playgroud)

java rest jersey jackson glassfish-3

39
推荐指数
4
解决办法
2万
查看次数

如何从http或https请求获取带端口的主机名

我在jboss容器中部署了两个应用程序(相同的unix框).如果我从app1收到请求,我需要为app2设置相应的请求.

例如:
如果app1请求是:http://example.com/context? param1 = 123
然后我需要提取" http://example.com/ ",以便我可以构建第二个应用程序的请求.

我试过用:

  HttpServletRequest.getServerName() & 
  HttpServletRequest.getServerPort() & \
  HttpServletRequest.getHeader("host") 
Run Code Online (Sandbox Code Playgroud)

方法,但请求可能是http或https.

如果还有其他更好的方法,请告诉我.谢谢!

java servlets http httprequest http-headers

29
推荐指数
5
解决办法
10万
查看次数

如何从HttpServletRequest获取请求URL

假设我发出这样的获取请求:

GET http://cotnet.diggstatic.com:6000/js/loader/443/JS_Libraries,jquery|Class|analytics|lightbox|label|jquery-dom|jquery-cookie?q=hello#frag HTTP/1.0
Host: cotnet.diggstatic.com:6000
Run Code Online (Sandbox Code Playgroud)

我的servlet接受这样的请求:HttpServletRequest req;

当我调试我的服务器并执行时,我得到以下内容:

req.getRequestURL().toString() = "http://cotnet.diggstatic.com:6000/js/loader/443/JS_Libraries,jquery%7cClass%7canalytics%7clightbox%7clabel%7cjquery-dom%7cjquery-cookie"
req.getRequestURI() = "/js/loader/443/JS_Libraries,jquery%7cClass%7canalytics%7clightbox%7clabel%7cjquery-dom%7cjquery-cookie"
req.getQueryString() = "q=hello"
Run Code Online (Sandbox Code Playgroud)

如何获取片段信息?此外,当我调试请求时,我看到一个类型为java.net.URI的uri_字段,其中包含片段信息.这正是我想要的.我怎么能得到它?

url uri servlets http

11
推荐指数
1
解决办法
3万
查看次数

如何将 URL 作为路径变量传递

我试图将 URL 作为路径变量传递,但是当我输入 URL 作为参数并访问路由时,它返回错误。我希望在它作为参数传入路由后能够看到地址。

@RequestMapping("/addAddress/{address}")
public String addAddress(@PathVariable("address") String address) {
    System.out.println("Address: "+address)
    return address;
}
Run Code Online (Sandbox Code Playgroud)

例如,如果我输入 URL:

localhost:8080/addAddress/http://samplewebsite.com
Run Code Online (Sandbox Code Playgroud)

我应该看到

http://samplewebsite.com 
Run Code Online (Sandbox Code Playgroud)

在后端打印出来。

java spring-boot

5
推荐指数
1
解决办法
5828
查看次数

如何在Spring Boot中获取请求URL

我需要将请求URL作为String参数提交给方法

@RequestMapping(value = "/test", method = RequestMethod.POST)
public void testItt(@RequestParam String requestParameter, @RequestURL String requestUrl) { 
   // Do something with requestUrl 
} 
Run Code Online (Sandbox Code Playgroud)

如何正确提交请求URL?

我试过了

@RequestMapping(value = "/test", method = RequestMethod.POST)
public void testItt(@RequestParam String requestParameter, @RequestURL String requestUrl) { 
   // Do something with requestUrl 
} 
Run Code Online (Sandbox Code Playgroud)

但我觉得必须有更好的方法

spring spring-boot

2
推荐指数
1
解决办法
1249
查看次数

如何从 HttpServletRequest 获取完整的 url?

好的,我有这些代码:

HttpServletRequest httpRequest = (HttpServletRequest) request;
String requestQueryString = httpRequest.getQueryString();
System.out.println(requestQueryString);
Run Code Online (Sandbox Code Playgroud)

打开网址“ http://127.0.0.1:8888/MyProject.html?gwt.codesvr=127.0.0.1:9997?_escaped_fragment_=home”时,

它打印出来:

 gwt.codesvr=127.0.0.1:9997?_escaped_fragment_=home
Run Code Online (Sandbox Code Playgroud)

该网址缺少该http://127.0.0.1:8888/MyProject.html?部分。

如何解决?

java servlets

1
推荐指数
1
解决办法
8437
查看次数