我使用dropwizard framework实现了Resful应用程序。我使用dropwizard-auth-jwt进行maven包验证:
<依赖性>
<&的groupId GT; com.github.toastshaman< /&的groupId GT;
< artifactId的> dropwizard-AUTH-JWT< / artifactId的>
< version> 1.0.2-0< / version>
< /依赖性>
为了添加资源认证,我实现了sampleAuthenticator,它是一个Authenticator类,它使用Principal类进行身份验证检查。
public class UserAuthenticate implements Authenticator <JwtContext, MyUser> {
@Override
public Optional<MyUser> authenticate(JwtContext context) {
try {
final String subject = context.getJwtClaims().getSubject();
if ("authentication".equals(subject)) {
return Optional.of(new MyUser("admin", "pass"));
}
return Optional.empty();
}
catch (MalformedClaimException e) { return Optional.empty(); }
}
}
当MyUser实施校长时:
public class MyUser implements Principal {
private String pass;
private String name;
public MyUser(String name, String pass) {
this.pass = pass;
this.name = name;
}
public MyUser( String name){
this.name = name;
}
public MyUser(){}
public String getPass() {
return pass;
}
@Override
public String getName() {
return name;
}
@Override
public String toString() {
return "MyUser{" +
"pass='" + pass + '\'' +
", name='" + name + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final MyUser myUser = (MyUser) o;
return Objects.equals(pass, myUser.pass) && Objects.equals(name, myUser.name);
}
@Override
public int hashCode() {
return Objects.hash(pass, name);
}
}
通过这种配置,我需要为crud操作添加资源。获取和删除没有问题。但是当我添加帖子或者当需要为请求体添加新对象时,我有错误。
后:
@POST
@Path("/")
public Response create(@Auth MyUser admin, Body body) {
return Response
.status(Response.Status.OK)
.type(MediaType.APPLICATION_JSON)
.entity(true)
.build();
}
错误:
引起:org.glassfish.jersey.server.model.ModelValidationException: 在此期间,应用程序资源模型的验证失败 应用初始化。 [[致命]没有找到注射源 public javax.ws.rs.core.Response类型的参数 myGroup.resources.BodyResource.create(myGroup.api.MyUser,myGroup.api.Body) 在索引0。源=&#39; {ResourceMethod =列举HTTPMethod POST, consumeTypes = [application / json],producedTypes = [application / json], suspended = false,suspendTimeout = 0,suspendTimeoutUnit = MILLISECONDS, 可调用= Invocable的{处理机= ClassBasedMethodHandler {handlerClass =类 myGroup.resources.BodyResource, handlerConstructors = [org.glassfish.jersey.server.model.HandlerConstructor@20411320]}, definitionMethod = public javax.ws.rs.core.Response myGroup.resources.BodyResource.create(myGroup.api.MyUser,myGroup.api.Body), parameters = [Parameter [type = class myGroup.api.MyUser,source = null, defaultValue = null],参数[type = class myGroup.api.Body, source = null,defaultValue = null]],responseType = class javax.ws.rs.core.Response},nameBindings = []}&#39;,[警告] (sub)资源方法在myGroup.resources.BodyResource中创建包含 空路径注释。 source =&#39; public javax.ws.rs.core.Response myGroup.resources.BodyResource.create(myGroup.api.MyUser,myGroup.api.Body)&#39;
答案 0 :(得分:1)
Jersey
希望@Path
注释处于班级。请在此处查看我的回答:Parse request parameters without writing wrapper class
我不知道你使用的是哪种版本的dropwizard,但我不知道 将@POST和@Path(“/ something”)注释组合在一起 在注释方法时表现。我收到了HTTP ERROR 404。