
Ich erstelle eine REST-Anwendung auf einem Tomcat 7-Server.
Verwenden der Anwendungsklasse für Bootstrap.
Implementierung der Anwendungsklasse wie folgt. Erstellen eines Singleton-Objekts von Resource01.
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import com.somename.api.Resource01;
@ApplicationPath("/service")
public class AppConfig extends Application{
public Set<Object> all_resources;
@Override
public Set<Object> getSingletons()
{
all_resources = new HashSet<Object>();
Resource01 z = new Resource01();
all_resources.add(z);
return all_resources;
}
}
Die Ressourcenklasse ist wie folgt.
Ich biete Unterstützung für 2 HTTP-Methoden PUT und GET.
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("resource")
public class Resource01 {
@GET
@Path("mailer_status")
@Produces(MediaType.TEXT_HTML)
public String getMailStatus()
{
return "<h2>Mailer is in progress 14.47</h2>";
}
@PUT
@Path("create_config")
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.TEXT_PLAIN)
public String getMailConfig(String config)
{
//String instance_rep = m_config.toString();
return "Payload Delivered";
}
}
tomcat-users.xml lautet wie folgt.
<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
<role rolename="manager-gui"/>
<user username="tomcat" password="tomcat" roles="manager-gui"/>
</tomcat-users>
Die Datei befindet sich unter %CATALINA_HOME%/conf
Ich habe ein wenig gesurft/gebrowst, um den Fehlergrund zu finden, und basierend darauf habe ich web.xml geändert, das sich in %CATALINA_HOME%/webapps/manager/WEB-INF befindet
<security-constraint>
<web-resource-collection>
<web-resource-name>Admin</web-resource-name>
<url-pattern>/*</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
<http-method>PUT</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>manager-gui</role-name>
</auth-constraint>
<!-- Specifying a Secure Connection -->
<user-data-constraint>
<!-- transport-guarantee can be CONFIDENTIAL (forced SSL), INTEGRAL, or NONE -->
<transport-guarantee>NONE</transport-guarantee>
</user-data-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>Tomcat Manager Application</realm-name>
</login-config>
Dann exportiere ich das dynamische Webprojekt als War-Datei und stelle die War-Datei bereit. Zu Testzwecken wird ARC (Advanced Rest Client) verwendet.
Zuerst habe ich eine GET-Anfrage ausprobiert – ich erhalte eine Antwort. Screenshot beigefügt.
GET-Anforderung
Wie im Screenshot zu sehen ist, wurde die Antwort 200 OK empfangen. Ich kann die gewünschte Zeichenfolge im Browser sehen.
Wenn ich eine PUT-Anfrage versuche, erhalte ich den Fehler 403 Forbidden. Ich füge einige Screenshots bei, die das beschreiben.
PUT_REQUEST_HEADER
PUT_REQUEST-NUTZLAST
PUT_RESPONSE
Jede Hilfe/Anleitung ist willkommen.
Grüße,
Ashish
Antwort1
ich habe mir die Datei web.xml im Ordner „conf“ nicht angesehen.
da hatten wir das
<security-constraint>
<web-resource-collection>
<web-resource-name>restricted methods</web-resource-name>
<url-pattern>/*</url-pattern>
<http-method>TRACE</http-method>
<http-method>PUT</http-method>
<http-method>DELETE</http-method>
<http-method>OPTIONS</http-method>
</web-resource-collection>
<auth-constraint />
</security-constraint>
Ich habe die PUT-Methode kommentiert.
<security-constraint>
<web-resource-collection>
<web-resource-name>restricted methods</web-resource-name>
<url-pattern>/*</url-pattern>
<http-method>TRACE</http-method>
<!--<http-method>PUT</http-method>-->
<http-method>DELETE</http-method>
<http-method>OPTIONS</http-method>
</web-resource-collection>
<auth-constraint />
</security-constraint>
und es hat funktioniert !