2017-09-01 42 views
1

這是我在Java中的第一個API,請記住這一點;)我有Controller.class,現在我正在保留所有方法(稍後我會清理它;) )。至於GET方法完美工作,當我想創建新用戶時,PUT方法有問題 - 它不起作用。請幫忙嗎?和建議的原因 - 正如我所說的 - 我是新手,當談到API)Java API/Jersey放置方法

所以這是我controller.class:

@Path("/api") 
public class Controller { 

    List<Post> mBartoszPosts; 
    List<Post> mFelipePosts; 
    List<Post> mShawnPosts; 
    List<Post> mDavidPosts; 
    List<Post> mGraziellaPosts; 
    List<Post> mAllPosts; 


    List<User> mUsers; 
    User bartosz; 
    User felipe; 
    User shawn; 
    User david; 
    User graziella; 



     @Path("/user/{id}") 
     @GET 
     @Produces("application/json") 
    public Response getUser(@PathParam("id")int id) { 
     setUpUsers(); 
     System.out.println("Liczba osob : " + mUsers.size()); 
     for(User user : mUsers) { 
      if(id == user.getId()) { 
       String result = user.toString(); 
      return Response.status(200).entity(user).build(); 
      } 
     } 
     return null; 
     } 
     @Path("/post/{post_id}") 
     @GET 
     @Produces("application/json") 
    public Response getPost(@PathParam("post_id")int post_id) { 
     setUpUsers(); 
     System.out.println("Liczba osob : " + mUsers.size()); 
     for(Post post : mAllPosts) { 
      if(post_id == post.getId()) { 
       String result = post.toString(); 
      return Response.status(200).entity(post).build(); 
      } 
     } 
     return null; 

     } 
     @Path("/posts") 
     @GET 
     @Produces("application/json") 
    public Response getPosts() { 
     setUpUsers(); 
     String response = new Gson().toJson(mAllPosts); 
      return Response.status(200).entity(response).build(); 

     } 

     @PUT 
     @Path("user/new/{id}/{post}") 
     @Consumes(MediaType.APPLICATION_XML) 
     @Produces(MediaType.APPLICATION_XML) 
     public Response updateEmployeeById(@PathParam("id") Integer id,@PathParam("post") String userPost) 
     { 
      List<Post>userPosts = new ArrayList(); 
      Post post = new Post(99,userPost,"Bartosz"); 
      userPosts.add(post); 
      User updatedEmployee = new User(id,"Bartek","Szlapa",userPosts); 

      if(updatedEmployee.getName() == null) { 
       return Response.status(400).entity("Please provide the employee name !!").build(); 
      } 

      updatedEmployee.setId(id); 
      updatedEmployee.setName(updatedEmployee.getName()); 
      System.out.println(updatedEmployee.getName()); 

      return Response.ok().entity(updatedEmployee).build(); 
     } 

     public int maxValue(int array[]){ 
      int max = Arrays.stream(array).max().getAsInt(); 
      return max; 
     } 
} 

正如你所看到的最後一個方法是PUT和它doesn」將不起作用:這是我的測試客戶端:

public class Test { 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 

     Client client = ClientBuilder.newClient(new ClientConfig().register(Controller.class)); 
     WebTarget webTarget = client.target("http://localhost:8080/JerseyDemos/rest").path("api").path("user").path("new").path("77"); 
     List<Post>userPosts = new ArrayList(); 
     Post post = new Post(99,"Bartek Szlapa","Testing post ..."); 
     userPosts.add(post); 
     User emp = new User(99,"Bartek","Szlapa",userPosts); 
     Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_XML); 
     Response response = invocationBuilder.put(Entity.entity(emp, MediaType.APPLICATION_XML)); 
     User user = response.readEntity(User.class); 
     System.out.println(response.getStatus()); 
     System.out.println(user); 
    } 
} 

最後我的錯誤:

Exception in thread "main" org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/xml, type=class entities.User, genericType=class entities.User. 
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:248) 
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:163) 
    at org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1135) 
    at org.glassfish.jersey.client.ClientRequest.doWriteEntity(ClientRequest.java:516) 
    at org.glassfish.jersey.client.ClientRequest.writeEntity(ClientRequest.java:498) 
    at org.glassfish.jersey.client.internal.HttpUrlConnector._apply(HttpUrlConnector.java:384) 
    at org.glassfish.jersey.client.internal.HttpUrlConnector.apply(HttpUrlConnector.java:282) 
    at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:278) 
    at org.glassfish.jersey.client.JerseyInvocation.lambda$invoke$0(JerseyInvocation.java:753) 
    at org.glassfish.jersey.internal.Errors.process(Errors.java:316) 
    at org.glassfish.jersey.internal.Errors.process(Errors.java:298) 
    at org.glassfish.jersey.internal.Errors.process(Errors.java:229) 
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:414) 
    at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:752) 
    at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:445) 
    at org.glassfish.jersey.client.JerseyInvocation$Builder.put(JerseyInvocation.java:334) 
    at test.Test.main(Test.java:33) 

預先感謝幫助!

回答

1

似乎在客戶端代碼2個問題 -

  1. 爲了消除這種錯誤,你應該在你的classpath中添加jersey-media-jaxbJAR。如果您正在使用maven,你pom.xml添加下面的依賴 -

    <dependency> 
        <groupId>org.glassfish.jersey.media</groupId> 
        <artifactId>jersey-media-jaxb</artifactId> 
        <version>${jersey.version}</version> 
    </dependency> 
    
  2. 根據你PUT方法API規範 -

public Response updateEmployeeById(@PathParam("id") Integer id,@PathParam("post") String userPost) { 

你的REST服務只path parameteridpost需要。沒有其他輸入是必需的。然而,在你的客戶端代碼,您要發送User類對象emp對象同時呼籲您的API -

Response response = invocationBuilder.put(Entity.entity(emp, MediaType.APPLICATION_XML)); 

既然你不是你的API在收到此emp對象,所以這個數據會迷路。因此,要麼您應該更新您的API,以便在請求中接受User類對象,也不要從您的客戶端代碼發送此對象。