如何在Java中对接口进行注释?

huangapple go评论61阅读模式
英文:

How do I annotate on an interface in java?

问题

我正在尝试创建一个带有注解的接口,然而,当我尝试创建注解时,我收到了“不允许在此处使用注解”的消息。我不明白为什么会出现这个问题,因为我正在遵循一个做完全相同操作并且在教程中运行正常的教程。这可能看起来是一个愚蠢的问题,但我刚入门编程,尽管我已经尝试过研究为什么会出现这个问题,但没有找到任何能解答我问题的东西。所以有人能解释一下为什么我不能在我的接口中添加注解吗?

package com.myapp.groceryapp;

import retrofit2.http.Headers;
import retrofit2.http.Body;
import retrofit2.http.POST;

public interface APIService {

   @Headers(
          {
                "Content-Type:application/json",
                "Authorization:AAAAwg8PTZg:APA91bEY2M- 
   GHRhnKptu3VuNaRK3daxSdLf1S8p5ltF8wCvQ5ylGFmSXNCeXnTfymzN5VqWpucoYn8T6wq2ffR6i45sFRO2GE7"
          }
 )

   @POST("fcm/send")
}
英文:

I am trying to create an interface with an annotation, however, when I try to create the annotation I receive Annotations are not allowed here. I don't understand why I am receiving this as I am following a tutorial that does the exact same thing and it works in the tutorial. This may seem like a dumb question but I am new to programming & I have tried researching why I am receiving this problem but found nothing that answers my question. So can someone please explain to me why I cannot annotate in my interface?

package com.myapp.groceryapp;

import retrofit2.http.Headers;
import retrofit2.http.Body;
import retrofit2.http.POST;

public interface APIService {

   @Headers(
          {
                "Content-Type:application/json",
                "Authorization:AAAAwg8PTZg:APA91bEY2M- 
   GHRhnKptu3VuNaRK3daxSdLf1S8p5ltF8wCvQ5ylGFmSXNCeXnTfymzN5VqWpucoYn8T6wq2ffR6i45sFRO2GE7"
          }
 )

   @POST ("fcm/send")
}

答案1

得分: 0

你应该在接口内部注释函数。例如:

 @Headers(
          {
                "Content-Type:application/json",
                "Authorization:AAAAwg8PTZg:APA91bEY2M- 
   GHRhnKptu3VuNaRK3daxSdLf1S8p5ltF8wCvQ5ylGFmSXNCeXnTfymzN5VqWpucoYn8T6wq2ffR6i45sFRO2GE7"
          }
 )

   @POST ("fcm/send")
Call<playlist> func(@Body fooParam param);
英文:

You should annotate functions inside interfaces.
for instance:

 @Headers(
          {
                &quot;Content-Type:application/json&quot;,
                &quot;Authorization:AAAAwg8PTZg:APA91bEY2M- 
   GHRhnKptu3VuNaRK3daxSdLf1S8p5ltF8wCvQ5ylGFmSXNCeXnTfymzN5VqWpucoYn8T6wq2ffR6i45sFRO2GE7&quot;
          }
 )

   @POST (&quot;fcm/send&quot;)
Call&lt;playlist&gt; func(@Body fooParam param);

huangapple
  • 本文由 发表于 2020年10月7日 17:09:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/64240853.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定