我正在尝试使用Spring进行REST调用.据我所知,正确的方法是使用RestTemplate(?).问题是,我是一个代理人.
这是我现在的代码:
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
InetSocketAddress address = new InetSocketAddress(host, 3128);
Proxy proxy = new Proxy(Proxy.Type.HTTP, address);
factory.setProxy(proxy);
RestTemplate restTemplate = new RestTemplate();
restTemplate.setRequestFactory(factory);
Run Code Online (Sandbox Code Playgroud)
似乎工作,但我需要在代理验证,但这是如何完成的?该Proxy类型以及该SimpleClientHttpRequestFactory类型似乎不处理凭据.没有凭据,我只得到407 ......
使用Springs自动实现的存储库是否可以限制findAll方法的结果大小?
我正在尝试在界面中声明以下内容:
List<XY> findAllTopTen();
Run Code Online (Sandbox Code Playgroud)
不幸的是,它不起作用(这)方式......
来自https://spring.io/guides/gs/centralized-configuration/:
您可以通过使用 Spring Cloud Config @RefreshScope 注解 MessageRestController 然后触发刷新事件来强制 Bean 刷新其配置(即从配置服务器中提取更新的值)。
我们如何触发这个刷新事件(对于用@ConfigurationProperties和注释的类@RefreshScope)?我们不想使用弹簧执行器/refresh端点。应用程序从配置服务器获取配置需要什么?
我创建了以下调用类,当调用被拦截的方法时,应该调用它:
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;
@Interceptor
class TestAspect {
@AroundInvoke
public Object log(InvocationContext context) throws Exception {
System.out.println("AroundInvoke method called");
return context.proceed();
}
}
Run Code Online (Sandbox Code Playgroud)
和这个资源:
import javax.interceptor.Interceptors;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
@Path("/test")
@Interceptors(TestAspect.class)
public class TestResource {
@GET
@Path("/")
public String test() {
System.out.println("Resource method called");
return new String("test");
}
}
Run Code Online (Sandbox Code Playgroud)
但我只从资源中获取日志行。
是否可以在不使用异常的情况下使用 Spring RestTemplate 来处理状态为 500 的 http 响应?
RestTemplate restTemplate = new RestTemplate();
try {
response = restTemplate.getForEntity(probe.getUrl(), String.class);
boolean isOK = response.getStatusCode() == HttpStatus.OK;
// would be nice if 500 would also stay here
}
catch (HttpServerErrorException exc) {
// but seems only possible to handle here...
}
Run Code Online (Sandbox Code Playgroud) application.yaml:
proxy.http.host: localhost
proxy.http.port: 3128
Run Code Online (Sandbox Code Playgroud)
配置类:
@Component
@ConfigurationProperties(prefix = "proxy.http")
data class ProxyConfig(var host: String = "", var port: Int = -1) {
}
Run Code Online (Sandbox Code Playgroud)
消费者(一种object)
object RestUtils {
@Autowired
lateinit var proxyConfig: ProxyConfig
fun createRestTemplate(): RestTemplate {
val proxy = Proxy(Proxy.Type.HTTP, InetSocketAddress(proxyConfig.host, 3128))
Run Code Online (Sandbox Code Playgroud)
不幸的是,它在运行时以kotlin.UninitializedPropertyAccessException异常结束.
堆栈跟踪:
kotlin.UninitializedPropertyAccessException: lateinit property proxyConfig has not been initialized
at com.test.infrastructure.RestUtils.createRestTemplate(RestUtils.kt:16) ~[classes/:na]
at com.test.monitoring.MonitoringController.getComponentsStatus(MonitoringController.kt:36) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_20]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_20]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_20]
at java.lang.reflect.Method.invoke(Method.java:483) ~[na:1.8.0_20]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221) ~[spring-web-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at …Run Code Online (Sandbox Code Playgroud)