
我正在 Tomcat 7 伺服器上建立 REST 應用程式。
使用應用程式類別進行引導。
應用程式類別的實作如下。建立 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;
}
}
資源類別如下。
我提供 2 個 http 方法 PUT 和 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如下。
<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
<role rolename="manager-gui"/>
<user username="tomcat" password="tomcat" roles="manager-gui"/>
</tomcat-users>
文件位於%CATALINA_HOME%/conf
由於失敗原因,我進行了一些衝浪/瀏覽,並基於此修改了位於 %CATALINA_HOME%/webapps/manager/WEB-INF 中的 web.xml
<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>
然後我將動態 Web 專案匯出為 war 檔案並部署該 war 檔。出於測試目的,使用 ARC(高級休息客戶端)。
首先我嘗試了 GET 請求 - 收到回應。附截圖。
獲取請求
從螢幕截圖中可以看出,收到了 200 OK 回應。我可以在瀏覽器中看到所需的字串。
當我嘗試 PUT 請求時。我收到 403 禁止錯誤。我附上一些描述相同的螢幕截圖。
PUT_REQUEST_HEADER
PUT_REQUEST-有效負載
PUT_RESPONSE
任何幫助/指導表示讚賞。
問候,
阿什什
答案1
我沒有查看conf資料夾下保存的web.xml。
我們有這個
<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>
我評論了 PUT 方法。
<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>
它起作用了!