有没有办法在Spring应用程序中静态/全局请求ApplicationContext的副本?
假设主类启动并初始化应用程序上下文,是否需要将它通过调用堆栈传递给任何需要它的类,或者有没有办法让类询问先前创建的上下文?(我认为必须是单身?)
我一直在使用Spring RestTemplate一段时间,当我试图调试它的请求和响应时,我一直碰壁.我基本上希望看到当我使用curl并打开"详细"选项时看到的相同内容.例如 :
curl -v http://twitter.com/statuses/public_timeline.rss
Run Code Online (Sandbox Code Playgroud)
将显示发送的数据和接收的数据(包括标题,cookie等).
我检查了一些相关的帖子,如: 我如何在Spring RestTemplate中记录响应? 但我没有设法解决这个问题.
实现此目的的一种方法是实际更改RestTemplate源代码并在那里添加一些额外的日志记录语句,但我会发现这种方法确实是最后的手段.应该有一些方法告诉Spring Web Client/RestTemplate以更友好的方式记录所有内容.
我的目标是能够使用以下代码执行此操作:
restTemplate.put("http://someurl", objectToPut, urlPathValues);
Run Code Online (Sandbox Code Playgroud)
然后在日志文件或控制台中获取相同类型的调试信息(我使用curl).我相信这对使用Spring RestTemplate且有问题的任何人都非常有用.使用curl调试RestTemplate问题不起作用(在某些情况下).
我用谷歌搜索找不到权威的答案.在Java servlet中,可以通过response.getOutputStream()或response.getWriter()访问响应主体.应该在写入之后在此流上调用.close()吗?
一方面,Blochian劝告总是关闭输出流.另一方面,我认为在这种情况下不存在需要关闭的底层资源.套接字的打开/关闭在HTTP级别进行管理,以允许诸如持久连接之类的事情.
我正在尝试org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping在我的 spring mvc 控制器中自动装配以获取所有 url 映射并将它们显示在 UI 上,但没有成功。有错误,bean 丢失:
org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping web.controller.WorkController.handlerMapping; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
Run Code Online (Sandbox Code Playgroud)
我的 web.xml:
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/root-context.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
Run Code Online (Sandbox Code Playgroud)
我的 mvc-dispatcher-servlet.xml:
<context:annotation-config/>
<context:component-scan …Run Code Online (Sandbox Code Playgroud) 我已经设置了 spring security 来验证和授权进入我的应用程序的请求。我已将配置设置如下:
public class OAuth2ServerConfiguration extends ResourceServerConfigurerAdapter {
@Override
public void configure(ResourceServerSecurityConfigurer resources) {
// ...set up token store here
resources.authenticationEntryPoint(new AuthenticationEntryPoint() {
@Override
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
//QUESTION
// How do I get the destination controller that this request was going to go to?
// Really, I'd like to get some information about the annotations that were on the destination controller.
response.setStatus(401);
}
});
}
Run Code Online (Sandbox Code Playgroud)
我想获取有关该请求将要发送到的目标控制器的一些信息。在这种情况下,控制器实际上不会受到攻击,因为 spring 安全性在响应到达控制器之前启动并丢弃了响应。
有小费吗?谢谢!
java ×5
spring ×2
debugging ×1
logging ×1
outputstream ×1
resttemplate ×1
servlets ×1
spring-mvc ×1