【问题标题】:Error No primary or default constructor found for interface javax.ws.rs.core.SecurityContext错误找不到接口 javax.ws.rs.core.SecurityContext 的主要或默认构造函数
【发布时间】:2019-06-23 18:35:10
【问题描述】:

我对 javax.ws.rs 的 SecurityContext 有疑问。

在我的控制器中,我用注解 @Context 注入 SecurityContext。

@RequestMapping(value = "/movie/search")
public String getMovies(@RequestParam(name = "search") String search, Model model, @Context SecurityContext securityContext) throws IOException {
    String token = s.getToken(securityContext);
    String res = s.getMovies(search,token);
    List<Movie> movies = parser.JsonToObject(res);
    model.addAttribute("movies", movies);
    return "movieList";

}

在 pom.xml 中我添加了依赖:

<dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>javax.ws.rs-api</artifactId>
        <version>2.0</version>
    </dependency>

但是当我尝试使用这个 API 时,我得到了错误:

java.lang.NoSuchMethodException: javax.ws.rs.core.SecurityContext.<init>()
at java.lang.Class.getConstructor0(Unknown Source) ~[na:1.8.0_201]
at java.lang.Class.getDeclaredConstructor(Unknown Source) ~[na:1.8.0_201]
at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttribute(ModelAttributeMethodProcessor.java:208) ~[spring-web-5.0.12.RELEASE.jar:5.0.12.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.createAttribute(ServletModelAttributeMethodProcessor.java:84) ~[spring-webmvc-5.0.12.RELEASE.jar:5.0.12.RELEASE]
at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:131) ~[spring-web-5.0.12.RELEASE.jar:5.0.12.RELEASE]

【问题讨论】:

  • 所以,提供一个非参数的公共构造函数
  • 我不明白。把构造函数放在哪里?
  • 在您的错误消息声称它丢失的类中 => "错误未找到接口 javax.ws.rs.core.SecurityContext 的主要或默认构造函数"

标签: java security security-context


【解决方案1】:

我想,您需要为您的 SecurityContext 实现提供一个公共的无参数构造函数。

【讨论】:

    猜你喜欢
    • 2020-11-17
    • 2019-02-20
    • 2018-11-13
    • 2019-07-06
    • 2018-08-16
    • 2019-12-18
    • 2019-05-06
    • 2023-03-20
    • 2012-05-12
    相关资源
    最近更新 更多