我有一个Maven项目,并且使用TomEE服务器。我有一个JAX-RS服务。我想将HATEOAS链接添加到响应中,并且使用类Link。
我的pom.xml是
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.readlearncode</groupId>
<artifactId>dukesbookshop</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>rest-server</artifactId>
<packaging>war</packaging>
<name>rest-server</name>
<url>http://localhost:8080</url>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<java.version>1.8</java.version>
<javaee-api.version>7.0</javaee-api.version>
</properties>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>${javaee-api.version}</version>
</dependency>
<dependency>
<groupId>org.apache.openejb</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0-6</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.ws.rs/javax.ws.rs-api -->
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0.1</version>
</dependency>
</dependencies>
<build>
<finalName>rest-server</finalName>
<plugins>
<plugin>
<groupId>org.apache.openejb.maven</groupId>
<artifactId>tomee-maven-plugin</artifactId>
<version>1.7.1</version>
<configuration>
<tomeeVersion>1.7.1</tomeeVersion>
<tomeeClassifier>plus</tomeeClassifier>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<filteringDeploymentDescriptors>true</filteringDeploymentDescriptors>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<skipTests>true</skipTests>
<systemPropertyVariables>
<!-- This is needed to tell the unit tests which profile we are running. -->
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
</project>
启动服务器后,我的应用程序即会启动,但是当我向邮递员查询网址时,
http://localhost:8080/rest-server/api/books/2526987585
我在服务器控制台中看到错误
Caused by: java.lang.NoSuchMethodError: javax.ws.rs.ext.RuntimeDelegate.createLinkBuilder()Ljavax/ws/rs/core/Link$Builder;
at javax.ws.rs.core.Link.fromUri(Link.java:185)
服务中的说明是
Link self = Link.fromUri(uriInfo.getBaseUriBuilder()
.path(getClass())
.path(getClass(), "getBookByIsbn")
.build(book.get().getId()))
.rel("self")
.type("GET")
.build();
如何在响应中配置HATEOAS链接?