在Java中,是否可以定义一个接口,该接口具有接收实现类参数的方法?
接口:
public interface MyInterface {
public <T is/extends of the type of the implementing class> void method(T object);
}
Run Code Online (Sandbox Code Playgroud)
类:
public class A implements MyInterface {
public void method(A object) {
...
}
}
Run Code Online (Sandbox Code Playgroud)
我想要避免的是一个类可以用另一个类来实现MyInterface.
所以这不应该被允许:
public class A implements MyInterface<B> {
public void method(B object) {
...
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:
好吧,我试着以另一种方式描述我想要实现的目标.我希望有几个类具有接受该类类型的arg的方法.所以除了上面的A类之外,让我们说另一个看起来像这样的B类:
public class B implements MyInterface {
public void method(B object) {
...
}
}
Run Code Online (Sandbox Code Playgroud)
展位类A和B的共同点是一种称为"方法"的方法,它接收类本身类型的arg.
在我的项目中,我希望实现以下目标.我正在写一个小游戏,并希望实现某种碰撞检测.基本上我想通过做一些"智能"多态调用来做到这一点.
我定义了一个接口ICollisionReceiver,如下所示:
public interface ICollisionReceiver<T extends IShape> extends IShape {
public boolean …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Haversine公式来查找具有Pageable的Spring Data JPA Query中某个位置附近的实体,但我没有完成它.
我的第一种方法看起来像这样
@Query("SELECT m, (6371 * acos(cos(radians(:latitude)) * cos(radians(m.latitude)) * cos(radians(m.longitude) - radians(:longitude)) + sin(radians(:latitude)) * sin(radians(m.latitude)))) as dist FROM Entity m WHERE dist < :distance ORDER BY dist DESC")
public List<Entity> findEntitiesByLocation(@Param("latitude") final double latitude, @Param("longitude") final double longitude, @Param("distance") final double distance, Pageable pageable);
Run Code Online (Sandbox Code Playgroud)
但它失败了,因为Spring/JPA似乎无法在where子句中使用别名.stacktrace中的SQL看起来像这样
select (...),(haversine) as col_1_0_ where dist<? order by col_1_0_ DESC
Run Code Online (Sandbox Code Playgroud)
所以where子句中的别名不会被替换.使用"col_1_0_"(不使用")代替dist也不起作用.
根据这个SO答案,至少MySQL是由内部解释的,并且在Where子句中使用别名是不可能的.建议的解决方案是使用HAVING而不是WHERE,但在HAVING子句中,别名不会被解析.
我知道我可以将Haversine公式移动到where子句中,但我仍然需要在Order By子句中使用它,我认为它可能会在Order By子句中使用相同的长Haversine公式来降低性能,因为我选择了几百个数千个实体.
然后我尝试手动创建查询,但我不知道如何将Pageable应用于此:
@Override
public List<Entity> findEntitiesByLocation(final double latitude, …Run Code Online (Sandbox Code Playgroud) 我想开发两个独立的服务,一个用于业务,一个用于使用Spring OAuth 2进行用户身份验证
我们称之为商业服务和OAuth服务.
现在,如果请求未经过身份验证,我希望将Business-Service委托给OAuth-Service.客户端应用程序(Android应用程序)不应该事先知道OAuth-Service,它应该只由Business-Service委托给它,并且302 HTTP重定向用于非认证请求.确切地说,我希望我的API登录页面提供指向http://businessservice.com/login的链接,当我的客户端应用程序决定关注此链接时,它会被重定向到OAuth-Service.
如果我使用@ EnableOAuth2Resource注释Business-Service ,那么当我在没有访问令牌的情况下卷曲它时,它的所有资源都会受到保护,返回401.到现在为止还挺好.如果我提供这样的访问令牌:
curl -v http://localhost:8667/resource/ -H "Authorization: Bearer $TOKEN"
Run Code Online (Sandbox Code Playgroud)
我可以访问该资源.还好.
但是,如果我使用@ EnableOAuth2Sso注释业务服务以启用重定向到OAuth服务,则会失去使用访问令牌访问资源的功能(与上面相同的卷曲),它只返回302到登录页面http: //本地主机:8667 /登录
如果我同时使用两个注释,@ EnableOAuth2Resource似乎总是"赢",因为身份验证有效,但调用http:// localhost:8667/login返回404.
那么创建一个委托给auth服务器进行非认证调用的资源服务器的正确方法是什么?
spring-security oauth-2.0 spring-security-oauth2 spring-cloud
我有一个关于 PromQL 及其查询函数rate() 以及如何正确使用它的问题。在我的应用程序中,我有一个线程正在运行,我使用 Micrometer 的计时器来监视线程的运行时间。使用 Timer 可以为您提供一个带有后缀 _count 的计数器和另一个带有后缀 _sum 所花费的秒数总和的计数器。例如 my_metric_sum 和 my_metric_count。
我的原始数据如下所示(抓取间隔 30 s,范围向量 5m):
现在根据文档,https://prometheus.io/docs/prometheus/latest/querying/functions/#rate 计算范围向量中时间序列的每秒平均增长率(此处为 5m)。
现在我的问题是:我为什么想要那个?我的执行运行时间的相对变化对我来说似乎毫无用处。事实上,仅使用 sum/count 看起来更有用,因为它为我提供了每个时刻的平均绝对持续时间。同时,这就是让我困惑的地方,在我找到的文档中
要根据名为 http_request_duration_seconds 的直方图或摘要计算过去 5 分钟内的平均请求持续时间,请使用以下表达式:
速率(http_request_duration_seconds_sum[5m]) / 速率(http_request_duration_seconds_count[5m])
来源: https: //prometheus.io/docs/practices/histograms/
但据我了解文档,这个表达式似乎会计算请求持续时间的每秒平均增长率,即不是请求平均花费多长时间,而是请求持续时间平均改变了多少最后5分钟。
我仍然不明白客户端在创建资源时如何知道要POST的数据.大多数教程/文章都忽略了这一点,在他们的例子中,客户似乎总是知道要发布什么(即使用带外信息).就像在这个例子中一样,消费者知道他必须通过设置他想要的<drink \>来下订单.
我只能想象一些方法,我不知道它们是否有效:
1.返回空资源
客户端发现指向/ resource的链接,其中包含指向/ resource/create和"create"的链接.GET to / resource/create返回一个空资源(所有属性都为空)和一个指向/ resource/create的链接"post".然后,客户端将值设置为所有属性,并将此POST发送到/ resource/create,返回201(已创建).这意味着CRUD操作不是位于资源端点,而是位于/ resource/create之类的URI ,并且客户端可能设置服务器忽略的属性(如服务器端设置的创建日期)
2.返回表单
基本上与上面相同的方法,尽管事实上不返回资源但是有关要发布哪些字段以及每个属性需要具有哪些数据类型的元信息.就像在这个例子中.但是,创建端点不位于/ resource,而是位于/ resource/create
3.通过更新
POST到/资源创建立即创建空资源并返回指向此资源的链接.然后,客户端可以按照此链接使用执行PUT的必要数据更新资源.
那么仍然遵循HATEOAs范例的最佳方法是什么?为什么所有这些教程(甚至像实践中的REST这样的书)都忽略了这个问题?
更新:
我最近发现Sun Cloud API似乎非常接近"理想的"REST HATEOAS API.它不仅定义了一些资源并在它们之间进行了超链接,还定义了媒体类型和版本控制.通过所有这些理论讨论,拥有一个具体的例子是非常好的.也许这有助于一些读者解决这个问题.
如何设置自定义登录表单以保护作为授权和资源服务器的Spring Boot应用程序中的/ oauth/authorize端点?我想实现用户必须登录才能向/ oauth/authorize发出请求.所有不是/ login和/ oauth/**的资源都应作为受OAuth保护的安全资源处理(需要有效的访问令牌)
因此,当用户调用localhost:1234/oauth/authorize?client_id = client&response_type = token&redirect_uri = http:// localhost:5678时,他首先被重定向到登录表单并成功登录后重定向到/ oauth/authorize端点,其中隐含OAuth流程继续进行.
当我尝试以下操作时,它使用标准的基本身份验证弹出窗口
@Configuration
@EnableAuthorizationServer
public class OAuthConfig extends AuthorizationServerConfigurerAdapter {
@Autowired
private AuthenticationManager authenticationManager;
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authenticationManager(authenticationManager);
}
@Override
public void configure(final ClientDetailsServiceConfigurer clients)
throws Exception {
clients.inMemory()
.withClient("client")
.authorizedGrantTypes("implicit", "refresh_token")
.scopes("read");
}
}
Run Code Online (Sandbox Code Playgroud)
资源配置
@Configuration
@EnableResourceServer
public class ResourceConfiguration
extends ResourceServerConfigurerAdapter
{
@Override
public void configure(final HttpSecurity http) throws Exception {
// @formatter:off
http.authorizeRequests().antMatchers("/login").permitAll().and()
.authorizeRequests().anyRequest().authenticated();
// @formatter:on
} …Run Code Online (Sandbox Code Playgroud) 我想为我的jax-ws webservice启用http压缩.我发现我必须使用可以修改http标头的自定义处理程序链.
我找到的所有教程都引用了注释@HandlerChain,指向处理程序链配置xml文件但我的问题是我的webservice必须尽可能轻量级,因此我无法在外部xml文件中定义我的处理程序链.
我尝试了以下但没有成功:
final Endpoint ep = Endpoint.publish("http://localhost:8878/mywebservice",
new WebserviceImpl() );
final Binding binding = ep.getBinding();
final List<Handler> handlerChain = binding.getHandlerChain();
handlerChain.add(new MySuperbSOAPHandler());
binding.setHandlerChain(handlerChain);
Run Code Online (Sandbox Code Playgroud)
有谁知道如何做到这一点?它甚至可能吗?
我想使用HTML5画布绘制图像,翻译图像然后更改图像,但保留我所做的转换.这可能吗?
这里有一些伪代码来说明我的问题:
// initially draw an image and translate it
var context = canvas.getContext("2d");
context.putImageData(someData, 0, 0);
context.translate(200, 10);
// then later somewhere else in code
// this should be drawn @ 200/10
var context = canvas.getContext("2d");
context.putImageData(someOtherData, ?, ?);
Run Code Online (Sandbox Code Playgroud)
我认为这可以通过一些保存/恢复调用,但我还没有成功,所以我怎么能实现这一点呢?
为了熟悉Spring Cloud的Eureka客户端/服务器机制,我尝试将客户端连接到Eureka服务器并每隔5分钟打开/关闭连接,以查看Eureka服务器如何处理此问题.
我有两个Eureka客户.
第一个是通过以下代码向我提供有关已注册应用程序的信息:
@Autowired
private DiscoveryClient discoveryClient;
@RequestMapping(value = "/services", produces = MediaType.APPLICATION_JSON)
public ResponseEntity<ResourceSupport> applications() {
ResourceSupport resource = new ResourceSupport();
Set<String> regions = discoveryClient.getAllKnownRegions();
for (String region : regions) {
Applications allApps = discoveryClient.getApplicationsForARegion(region);
List<Application> registeredApps = allApps.getRegisteredApplications();
Iterator<Application> it = registeredApps.iterator();
while (it.hasNext()) {
Application app = it.next();
List<InstanceInfo> instancesInfos = app.getInstances();
if (instancesInfos != null && !instancesInfos.isEmpty()) {
//only show one of the instances
InstanceInfo info = instancesInfos.get(0);
resource.add(new Link(info.getHomePageUrl(), "urls"));
}
}
}
return …Run Code Online (Sandbox Code Playgroud) spring service-discovery spring-boot spring-cloud netflix-eureka
我试图将一些Javascript函数推入一个数组,用jQuery.when()调用它们,但是当这样做时,函数在放入数组时就已经被调用了.这是一个例子,这是我的代码片段:
var answers = someJSONFormattedData;
var calls = [];
function callWithIndex(i) {
var answer = answers[i];
api.loadUser(answer.userType, answer.userId, function(userData) {
answer.user = userData;
}, null)
};
for(var i=0;i<answers.length;++i) {
calls.push(callWithIndex(i));
}
$.when.apply($, calls).then(function() {
var dataAsJSON = {
'answers': answers
};
//do some magic stuff with dataAsJSON
});
Run Code Online (Sandbox Code Playgroud)
即使在做一个简短的测试代码片段时,也会立即调用test()函数,尽管我根本不会调用它,因为我只将它放入一个数组中(或至少尝试).
var test = function(i) {
console.log("test: "+i);
}
var testArray = [];
for(var i=0;i<5;++i) {
testArray.push(test(i));
}
Run Code Online (Sandbox Code Playgroud) spring ×2
spring-cloud ×2
api ×1
arrays ×1
asynchronous ×1
canvas ×1
chain ×1
generics ×1
handler ×1
hateoas ×1
haversine ×1
html5 ×1
html5-canvas ×1
http ×1
java ×1
javascript ×1
jax-ws ×1
jpa ×1
jquery ×1
micrometer ×1
oauth ×1
oauth-2.0 ×1
post ×1
prometheus ×1
promql ×1
rest ×1
soap ×1
spring-boot ×1
spring-data ×1
types ×1