POST 操作失败,出现错误 405 方法不允许

POST Operation is failing with an error 405 Method not allowed

我使用下面 link 中描述的 S4SDK 开发了一个 Java 应用程序,并将其部署到 SAP Cloud 平台云铸造环境。

https://blogs.sap.com/2017/12/07/step-20-with-s4hana-cloud-sdk-create-and-deep-insert-with-the-virtual-data-model-for-odata/

然后我使用 POSTMAN 工具访问了该应用程序。首先,我为 application URL/businesspartners 发出了 GET 请求并获取了 x-csrf-token。我将此 x-csrf-token 用于 POST 操作,用于 application URL/businesspartners 和 body,如博客中所述(上面 link)。

所以,当我发出 post 请求时,它给出了一个错误:405 METHOD NOT ALLOWED

下面是 servlet:

@WebServlet("/businesspartners")
public class BusinessPartnerServlet extends HttpServlet {

private static final long serialVersionUID = 1L;
private static final Logger logger = CloudLoggerFactory.getLogger(BusinessPartnerServlet.class);

private static final String CATEGORY_PERSON = "1";

@Override
protected void doGet(final HttpServletRequest request, final HttpServletResponse response)
        throws ServletException, IOException {
    try {
        final List<BusinessPartner> businessPartners =
                new DefaultBusinessPartnerService()
                        .getAllBusinessPartner()
                        .select(BusinessPartner.BUSINESS_PARTNER,
                                BusinessPartner.LAST_NAME,
                                BusinessPartner.FIRST_NAME,
                                BusinessPartner.IS_MALE,
                                BusinessPartner.IS_FEMALE,
                                BusinessPartner.CREATION_DATE)
                        .filter(BusinessPartner.BUSINESS_PARTNER_CATEGORY.eq(CATEGORY_PERSON))
                        .orderBy(BusinessPartner.LAST_NAME, Order.ASC)
                        .execute();

        response.setContentType("application/json");
        response.getWriter().write(new Gson().toJson(businessPartners));

    } catch (final ODataException e) {
        logger.error(e.getMessage(), e);
        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        response.getWriter().write(e.getMessage());
    }
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    final String firstname = request.getParameter("firstname");
    final String lastname = request.getParameter("lastname");
    final String country = request.getParameter("country");
    final String city = request.getParameter("city");
    final String email = request.getParameter("email");

    //do consistency checks here...

    final AddressEmailAddress emailAddress = AddressEmailAddress.builder()
            .emailAddress(email)
            .build();

    final BusinessPartnerAddress businessPartnerAddress = BusinessPartnerAddress.builder()
            .country(country)
            .cityName(city)
            .toEmailAddress(Lists.newArrayList(emailAddress))
            .build();

    final BusinessPartnerRole businessPartnerRole = BusinessPartnerRole.builder()
            .businessPartnerRole("FLCU01")
            .build();

    final BusinessPartner businessPartner = BusinessPartner.builder()
            .firstName(firstname)
            .lastName(lastname)
            .businessPartnerCategory("1")
            .correspondenceLanguage("EN")
            .toBusinessPartnerAddress(Lists.newArrayList(businessPartnerAddress))
            .toBusinessPartnerRole(Lists.newArrayList(businessPartnerRole))
            .build();

    String responseBody;

    try {
        final BusinessPartner storedBusinessPartner = new StoreBusinessPartnerCommand(new ErpConfigContext(), new DefaultBusinessPartnerService(), businessPartner).execute();
        responseBody = new Gson().toJson(storedBusinessPartner);
        response.setStatus(HttpServletResponse.SC_CREATED);

    } catch(final HystrixBadRequestException e) {
        responseBody = e.getMessage();
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        logger.error(e.getMessage(), e);
    }

    response.setContentType("application/json");
    response.getOutputStream().print(responseBody);
}
}

这是我的 web.xml 文件。

<?xml version="1.0" encoding="UTF-8"?>

<session-config>
    <session-timeout>20</session-timeout>
</session-config>

<!--
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring-security.xml</param-value>
</context-param>
<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
-->

<filter>
    <filter-name>RestCsrfPreventionFilter</filter-name>
    <filter-class>org.apache.catalina.filters.RestCsrfPreventionFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>RestCsrfPreventionFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<filter>
    <filter-name>HttpSecurityHeadersFilter</filter-name>
    <filter-class>com.sap.cloud.sdk.cloudplatform.security.servlet.HttpSecurityHeadersFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>HttpSecurityHeadersFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<filter>
    <filter-name>HttpCachingHeaderFilter</filter-name>
    <filter-class>com.sap.cloud.sdk.cloudplatform.security.servlet.HttpCachingHeaderFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>HttpCachingHeaderFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

有人可以帮我吗?

您是否检查过您的 servlet 是否正确实现了 doPost 方法?以下关于 SO 的问题似乎很好:Java Servlet return error 405 (Method Not Allowed) for POST request.

您是否尝试过将 @Override 注释添加到 doPost 方法?