2

我使用下面链接中描述的 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 工具访问了该应用程序。首先,我发出了 GET 请求application URL/businesspartners并获取了x-csrf-token. 如博客(上面的链接)中所述,我将其x-csrf-token用于with body 的POST操作。application URL/businesspartners

所以,当我发出一个帖子请求时,它给出了一个错误: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>

有人可以帮我吗?

4

2 回答 2

0

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

于 2018-04-16T14:08:35.427 回答
0

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

于 2018-04-19T07:10:40.497 回答