У меня проблема с GlassFish, использующим IntellJ IDEA.Используя в этой среде библиотеку Джерси и спокойную архитектуру, когда я пытаюсь проверить работу CRUD, я не нахожу проблем с GET и POST.Вместо этого с PUT и DELETE сервер отвечает с ошибкой 403. Что я могу сделать?Есть ли способ решить проблему?
Это мой класс REST:
//restituisce la lista di utenti
@GET
@Path("/getlistanodi")
@Produces({"application/json", "application/xml"})
public Response getListaNodi() {
return Response.ok(NodiEdge.getInstance()).build();
}
@Path("/add")
@POST
@Consumes({"application/json", "application/xml"})
public Response addNodo(Nodo u) {
NodiEdge.getInstance().addNodo(u);
return Response.ok().build();
}
@Path("/remove/{idnodo}")
@DELETE
@Produces({"application/json", "application/xml"})
public Response removeNodo(@PathParam("idnodo") String id) {
NodiEdge.getInstance().removeNodo(id);
return Response.ok().build();
}
Это файл web.xml:
<filter>
<filter-name>UploadFilter</filter-name>
<filter-class>com.sun.webui.jsf.util.UploadFilter</filter-class>
<init-param>
<param-name>maxSize</param-name>
<param-value>-1</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>UploadFilter</filter-name>
<servlet-name>FacesServlet</servlet-name>
</filter-mapping>
<servlet>
<servlet-name>FacesServlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>ThemeServlet</servlet-name>
<servlet-class>com.sun.webui.theme.ThemeServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet>
<servlet-name>DownloadServlet</servlet-name>
<servlet-class>org.glassfish.admingui.common.servlet.DownloadServlet</servlet-class>
<init-param>
<param-name>ContentSources</param-name>
<param-value>
org.glassfish.admingui.common.servlet.LBConfigContentSource,
org.glassfish.admingui.common.servlet.ClientStubsContentSource,
org.glassfish.admingui.common.servlet.LogFilesContentSource
org.glassfish.admingui.common.servlet.LogViewerContentSource
</param-value>
</init-param>
<init-param>
<param-name>contentSourceId</param-name>
<param-value>LBConfig</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>DownloadServlet</servlet-name>
<url-pattern>/download/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>FacesServlet</servlet-name>
<url-pattern>/resource/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>FacesServlet</servlet-name>
<url-pattern>/html/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>FacesServlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>FacesServlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ThemeServlet</servlet-name>
<url-pattern>/theme/*</url-pattern>
</servlet-mapping>
<session-config>
<cookie-config>
<http-only>true</http-only>
</cookie-config>
</session-config>
<!-- following mapping added to avoid JSF warning. refer to GLASSFISH-19403 -->
<mime-mapping>
<extension>jsp</extension>
<mime-type>text/html</mime-type>
</mime-mapping>
<welcome-file-list>
<welcome-file>/index.jsf</welcome-file>
</welcome-file-list>
<error-page>
<exception-type>javax.faces.application.ViewExpiredException</exception-type>
<location>/</location>
</error-page>
<login-config>
<auth-method>FORM</auth-method>
<realm-name>admin-realm</realm-name>
<form-login-config>
<form-login-page>/login.jsf</form-login-page>
<form-error-page>/loginError.jsf</form-error-page>
</form-login-config>
</login-config>
<security-role>
<role-name>admin</role-name>
</security-role>
И яДобавить класс методов:
@XmlElement(name="nodi")
private List<Nodo> listaNodi;
private static NodiEdge instance;
private int distNodi;
public NodiEdge() {
listaNodi = new ArrayList<Nodo>();
}
//singleton
public synchronized static NodiEdge getInstance(){
if(instance==null)
instance = new NodiEdge();
return instance;
}
public synchronized List<Nodo> getListaNodi() {
return new ArrayList<>(listaNodi);
}
public synchronized List<Nodo> addNodo(Nodo u)
{
List<Nodo> copiaLista = getListaNodi();
if (listaNodi.size()==0)
listaNodi.add(u);
else {
for (Nodo n : copiaLista) {
if (n.getID().toLowerCase().equals(u.getID().toLowerCase())) {
System.err.println("Inserimento negato! Identificatore già esistente.");
break;
}
else {
distNodi = Math.abs((u.getX() - n.getX()) + (u.getY() - n.getY()));
if (distNodi < 20) {
System.err.println("Inserimento negato! Posizione troppo vicina ad altri nodi.");
break;
} else {
listaNodi.add(u);
System.out.println("ID nodo: " + n.getID() + ", indirizzo IP: " + n.getIPaddress() +
", n° porta: " + n.getPNd());
}
}
}
}
return copiaLista;
}
public synchronized void removeNodo(String id)
{
List<Nodo> copiaLista = getListaNodi();
if (listaNodi.size()==0)
System.err.println("Operazione impossibile! Nessun nodo nella griglia");
else {
for (Nodo n : copiaLista) {
if (!n.getID().toLowerCase().equals(id.toLowerCase()))
System.err.println("Nodo non presente nella griglia.");
else {
listaNodi.remove(n);
System.out.println("Nodo eliminato.");
break;
}
}
}