2017-01-30 97 views
0

我嘗試在我的web項目上使用REST。 POST工作,但DELETE和PUT不起作用,我會看到錯誤:HTTP狀態405 - 方法不允許。並獲得並不在所有的工作:REST - HTTP狀態405 - 不允許的方法

「& QUOT ; ID & QUOT ;:未在RFC 2068中定義,而不是由Servlet API支持說明:服務器不支持實現這一所需的功能請求。」

這是我的代碼:

package rest; 

import domain.model.Client; 

import javax.ejb.Stateless; 
import javax.persistence.EntityManager; 
import javax.persistence.PersistenceContext; 
import javax.ws.rs.*; 
import javax.xml.bind.annotation.XmlRootElement; 
import javax.ws.rs.core.GenericEntity; 
import javax.ws.rs.core.MediaType; 
import javax.ws.rs.core.Response; 

import java.util.ArrayList; 
import java.util.List; 

@XmlRootElement 
@Path("/clients") 
@Stateless 
public class ClientResources { 

@PersistenceContext 
EntityManager entityManager; 

@GET 
@Consumes(MediaType.APPLICATION_JSON) 
public Response getAll() { 
    List<Client> matchHistories = new ArrayList<>(); 
    for (Client m : entityManager 
      .createNamedQuery("client.all", Client.class) 
      .getResultList()) 
     matchHistories.add(m); 


    return Response.ok(new GenericEntity<List<Client>>(matchHistories) { 
    }).build(); 
} 

@POST 
@Consumes(MediaType.APPLICATION_JSON) 
public Response Add(Client client) { 

    entityManager.persist(client); 
    return Response.ok(client.getId()).build(); 
} 

@PUT 
@Path("/{id}") 
@Consumes(MediaType.APPLICATION_JSON) 
public Response update(@PathParam("id") int id, Client p) { 
    Client result = entityManager.createNamedQuery("client.id", Client.class) 
      .setParameter("clientId", id) 
      .getSingleResult(); 
    if (result == null) { 
     return Response.status(404).build(); 
    } 
    result.setName(p.getName()); 
    result.setSurname(p.getSurname()); 
    entityManager.persist(result); 
    return Response.ok().build(); 
} 

@GET 
@Path("/{id}") 
@Produces(MediaType.APPLICATION_JSON) 
public Response get(@PathParam("id") int id) { 
    Client result = entityManager.createNamedQuery("client.id", Client.class) 
      .setParameter("clientId", id) 
      .getSingleResult(); 
    if (result == null) { 
     return Response.status(404).build(); 
    } 
    return Response.ok(result).build(); 
} 

@DELETE 
@Path("/{id}") 
public Response delete(@PathParam("id") int id) { 
    Client result = entityManager.createNamedQuery("client.id", Client.class) 
      .setParameter("clientId", id) 
      .getSingleResult(); 
    if (result == null) 
     return Response.status(404).build(); 
    entityManager.remove(result); 
    return Response.ok().build(); 
} 

}

在郵差我寫了這個:

{ 
"id" : 1, 
"name" : "Adam" 
} 

enter image description here enter image description here

+0

我發現我的服務器只允許POST,GET,OPTIONS。我不知道如何改變這個配置。 Glassfish 4.0 – LuckyProgrammer

回答

0

檢查你的郵遞員。你應該把它設置爲下面的圖片。如果您的身體類型不是application/json,或者您的方法不是POST,您將收到Method not allowed錯誤。

Postman

要獲得HTTP /捲曲/ ...調用由郵差產生遵循這一形象。從路徑/clients/{id}達到

Generated Code

你PUT服務爲麥克在評論中發現。所以你必須使用客戶端的ID來調用它以使PUT正常工作。

The HTTP methods POST and PUT aren't the HTTP equivalent of the CRUD's create and update. They both serve a different purpose. It's quite possible, valid and even preferred in some occasions, to use PUT to create resources, or use POST to update resources.

Use PUT when you can update a resource completely through a specific resource. For instance, if you know that an article resides at http://example.org/article/1234 , you can PUT a new resource representation of this article directly through a PUT on this URL.

If you do not know the actual resource location, for instance, when you add a new article, but do not have any idea where to store it, you can POST it to an URL, and let the server decide the actual URL.

+0

向其餘服務提供CURL調用。你可以從郵差生成它。 –

+0

我該怎麼做? – LuckyProgrammer

+1

@YanaGlance你還在'/ clients'端點上做了一個'PUT',但是你還沒有用'@ PUT'定義任何方法來監聽那個端點(你有一個用於'/ clients/{id}' )。將郵遞員的'PUT'改爲'POST'。 – Mike

相關問題