SERVICE
@Path("/rest")
public class JustService {
@GET
@Produces("text/plain")
@Path("sayhi")
public String sayhi(){
return "Hi";
}
}
应用程序配置
@ApplicationPath("/*")
public class ApplicationConfig extends Application {
}
Tomcat在端口8080上运行,这是我正在发送的邮递员请求:localhost:8080/rest/sayhi
响应
"status": 404,
"error": "Not Found",
"message": "No message available",
这里是POM.XML:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
删除spring-boot-starter-web
依赖性,仅使用spring-boot-starter-jersey
依赖性。然后用JustService
注释@Component
类:
@Component
@Path("/rest")
public class JustService {
...
}
最后将以下类添加到您的应用程序:
@Component
public class JerseyConfig extends ResourceConfig {
public JerseyConfig() {
register(JustService.class);
}
}
有关更多详细信息,请参阅Spring Boot documentation。
您大声告诉其余的api查找服务的资源。
尝试将此方法添加到您的ApplicationConfig中:
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> set = new HashSet<>();
set.add(JustService.class);
return set;
}
如果不起作用,请尝试将您的ApplicationConfig映射到特定路径,例如:
@@ ApplicationPath(“ / jaxrs”)
当然,您的测试网址将会是
localhost:8080 / jaxrs / rest / sayhi
希望有帮助。