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

时间:2018-04-16 13:36:20

标签: s4sdk

我使用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-tokenPOST application URL/businesspartners操作405 METHOD NOT ALLOWED进行身体操作,如博客中所述(上面的链接)。

因此,当我发布帖子请求时,它会出错:@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); } }

以下是servlet:

<?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>

这是我的web.xml文件。

// User.php
    class User extends AppModel {
         public $actsAs = array('Containable');
         public $hasMany = array(
              'UserCategory'
         );
}

//UserCategory.php
    class UserCategory extends AppModel {
         public $actsAs = array('Containable');
         public $belongsTo = array(
              'Category'
         );
}
//Category.php
    class Category extends AppModel {
         public $actsAs = array('Containable');
         public $hasMany = array(
              'UserCategory'
         );
}

有人可以帮助我吗?

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:2)

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