0

I'm writing a rest client in maven project

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);
    }
}

and my resource is below and is consumes 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();
    }

}

and my pom.xml is

<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>

But when i am running my rest client, i am getting below error. I tried to debug and provide other dependencies too, still i'm not able to run this error out. I guess i'm still using the correct jersey version for method .request().get() in rest client. Can you help me on this.

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)
Farvardin
  • 5,336
  • 5
  • 33
  • 54
Mohit Darmwal
  • 275
  • 1
  • 5
  • 21

1 Answers1

0

Mostly you have jar version and package issue. Check this SO question. Also, decompile different versions of these jars and see which classes and methods are present and which are not and then use the relevant one. You can find good java decompilers online.

phoenixSid
  • 447
  • 1
  • 8
  • 22