我正在maven项目中编写一个休息客户端
public class MyRestClient {
public static void main(String[] args)
{
Client client= ClientBuilder.newClient();
//Response response= client.target("http://localhost/advanced-jaxrs-01/webapi/messages/1").request().get();
Response response= client.target("http://localhost:80/advanced-jaxrs-01/webapi/messages/1").request().get();
Message mess=response.readEntity(Message.class);
System.out.println(mess.getMessage());
System.out.println(response);
}
}
我的资源在下面并消耗JSON
@Path("/messages")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class MessageResource {
MessageService messageService = new MessageService();
@GET
public List<Message> getMessages(@BeanParam MessageFilterBean filterBean) {
if (filterBean.getYear() > 0) {
return messageService.getAllMessagesForYear(filterBean.getYear());
}
if (filterBean.getStart() >= 0 && filterBean.getSize() > 0) {
return messageService.getAllMessagesPaginated(filterBean.getStart(), filterBean.getSize());
}
return messageService.getAllMessages();
}
@POST
public Response addMessage(Message message, @Context UriInfo uriInfo) {
Message newMessage = messageService.addMessage(message);
String newId = String.valueOf(newMessage.getId());
URI uri = uriInfo.getAbsolutePathBuilder().path(newId).build();
return Response.created(uri)
.entity(newMessage)
.build();
}
@PUT
@Path("/{messageId}")
public Message updateMessage(@PathParam("messageId") long id, Message message) {
message.setId(id);
return messageService.updateMessage(message);
}
@DELETE
@Path("/{messageId}")
public void deleteMessage(@PathParam("messageId") long id) {
messageService.removeMessage(id);
}
@GET
@Path("/{messageId}")
public Message getMessage(@PathParam("messageId") long id, @Context UriInfo uriInfo) {
Message message = messageService.getMessage(id);
message.addLink(getUriForSelf(uriInfo, message), "self");
message.addLink(getUriForProfile(uriInfo, message), "profile");
message.addLink(getUriForComments(uriInfo, message), "comments");
return message;
}
private String getUriForComments(UriInfo uriInfo, Message message) {
URI uri = uriInfo.getBaseUriBuilder()
.path(MessageResource.class)
.path(MessageResource.class, "getCommentResource")
.path(CommentResource.class)
.resolveTemplate("messageId", message.getId())
.build();
return uri.toString();
}
private String getUriForProfile(UriInfo uriInfo, Message message) {
URI uri = uriInfo.getBaseUriBuilder()
.path(ProfileResource.class)
.path(message.getAuthor())
.build();
return uri.toString();
}
private String getUriForSelf(UriInfo uriInfo, Message message) {
String uri = uriInfo.getBaseUriBuilder()
.path(MessageResource.class)
.path(Long.toString(message.getId()))
.build()
.toString();
return uri;
}
@Path("/{messageId}/comments")
public CommentResource getCommentResource() {
return new CommentResource();
}
}
我的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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.practise.mohit</groupId>
<artifactId>advanced-jaxrs-01</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>advanced-jaxrs-01 Maven Webapp</name>
<url>http://maven.apache.org</url>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<!-- <artifactId>jersey-media-moxy</artifactId> -->
</dependency>
</dependencies>
<properties>
<!-- <jersey.version>2.16</jersey.version> -->
<jersey.version>2.16</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
但是当我正在运行我的休息客户端时,我遇到了错误。我试图调试并提供其他依赖项,但我仍然无法运行此错误。我想我仍然在休息客户端使用方法.request()。get()的正确球衣版本。你能帮我解决这个问题吗?
Exception in thread "main" java.lang.NoSuchMethodError: org.glassfish.jersey.CommonProperties.getValue(Ljava/util/Map;Ljavax/ws/rs/RuntimeType;Ljava/lang/String;Ljava/lang/Object;Ljava/lang/Class;)Ljava/lang/Object;
at org.glassfish.jersey.client.ClientConfig$State.initRuntime(ClientConfig.java:389)
at org.glassfish.jersey.client.ClientConfig$State.access$000(ClientConfig.java:87)
at org.glassfish.jersey.client.ClientConfig$State$3.get(ClientConfig.java:119)
at org.glassfish.jersey.client.ClientConfig$State$3.get(ClientConfig.java:116)
at org.glassfish.jersey.internal.util.collection.Values$LazyValueImpl.get(Values.java:322)
at org.glassfish.jersey.client.ClientConfig.getRuntime(ClientConfig.java:722)
at org.glassfish.jersey.client.ClientRequest.getConfiguration(ClientRequest.java:284)
at org.glassfish.jersey.client.JerseyInvocation.validateHttpMethodAndEntity(JerseyInvocation.java:126)
at org.glassfish.jersey.client.JerseyInvocation.<init>(JerseyInvocation.java:98)
at org.glassfish.jersey.client.JerseyInvocation.<init>(JerseyInvocation.java:91)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:399)
at org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:303)
at com.practise.restClient.MyRestClient.main(MyRestClient.java:16)
答案 0 :(得分:0)
大多数情况下,你有jar版本和包问题。检查this SO question。 此外,反编译这些jar的不同版本,并查看哪些类和方法存在,哪些不存在,然后使用相关的类。 你可以在网上找到好的java反编译器。