Rest API - NullPointerException in Broadleaf Commerce

自古美人都是妖i 提交于 2019-12-11 02:38:46

问题


I am trying to access this url for my autocomplete in the search box..

http://localhost:8080/api/v1/catalog/search/products?q=sauce

But, I am getting the following error.. It seems here that the

@Resource(name = "blExploitProtectionService")
protected ExploitProtectionService exploitProtectionService;

exploitProtectionService is null

And here is the error..

HTTP ERROR 500

Problem accessing /api/v1/catalog/search/products. Reason:

    Server Error

Caused by:

java.lang.NullPointerException
   at org.broadleafcommerce.core.web.api.endpoint.catalog.CatalogEndpoint.findProductsByQuery(CatalogEndpoint.java:190)
   at com.mycompany.api.endpoint.catalog.CatalogEndpoint.findProductsByQuery(CatalogEndpoint.java:75)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

My web.xml is as follows

    <servlet>
       <servlet-name>RESTApiServlet</servlet-name>
       <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>com.mycompany.api.endpoint</param-value>
        </init-param>
   </servlet>

   <servlet-mapping>
       <servlet-name>RESTApiServlet</servlet-name>
       <url-pattern>/api/v1/*</url-pattern>
   </servlet-mapping>

Next, my applicationContext.xml is

<context:component-scan base-package="org.broadleafcommerce.core.web.api"/>

and applicationContext-security.xml is

<!-- Set up Spring security for the RESTful API -->
<sec:http pattern="/api/**" create-session="stateless">
    <sec:http-basic />
    <sec:custom-filter ref="blRestCustomerStateFilter" after="REMEMBER_ME_FILTER"/>
</sec:http>

<!-- Used for REST api calls.   This just takes in the passed in customerId and uses it to establish the customer. -->
<!-- Additional considerations MUST be made for implementations that are allowing external access to APIs. -->
<bean id="blRestCustomerStateFilter" class="org.broadleafcommerce.profile.web.core.security.RestApiCustomerStateFilter"/>

How to solve this problem? How do I make the exploitProtectionService variable not null. How can it be initialized?

Thanks in advance.


回答1:


Search functionality in REST is done by solr. When you pass q="something", then it will be search by solr query.

So when you pass * in q like, q=*, then it will work. But when you pass q="sauce", then it won't work, To make it work you have to make solr indexing based on product name.

To do so, put above sql lines in your project's core/src/main/resources/sql/load_catalog_data.sql :

INSERT INTO BLC_FIELD (FIELD_ID, ENTITY_TYPE, PROPERTY_NAME, ABBREVIATION, SEARCHABLE, TRANSLATABLE, FACET_FIELD_TYPE) VALUES (1, 'PRODUCT', 'defaultSku.name', 'name', TRUE, TRUE, 's');

INSERT INTO BLC_FIELD_SEARCH_TYPES (FIELD_ID, SEARCHABLE_FIELD_TYPE) VALUES (1, 't');

Make sure after executing this lines, you get entry in your DB's BLC_FIELD and BLC_FIELD_SEARCH_TYPES tables.

After setting this, you have to modify solr configurations. To do so, open schema.xml from site/src/main/resources. Change default configurations to above lines,

<fieldType name="text_general" class="solr.TextField" positionIncrementGap="100">
  <analyzer type="index">
    <tokenizer class="solr.StandardTokenizerFactory" />
    <filter class="solr.WordDelimiterFilterFactory"
      generateWordParts="0"
      generateNumberParts="0"
      catenateWords="0"
      catenateNumbers="0"
      catenateAll="0"
      preserveOriginal="1"
      />
      <filter class="solr.LowerCaseFilterFactory"/>
      <filter class="solr.StopFilterFactory"/>
      <filter class="solr.PorterStemFilterFactory"/>
      <filter class="solr.EdgeNGramFilterFactory" minGramSize="1" maxGramSize="255"/>
    </analyzer>
  <analyzer type="query">
    <tokenizer class="solr.LowerCaseTokenizerFactory" />
    <filter class="solr.WordDelimiterFilterFactory"
      generateWordParts="0"
      generateNumberParts="0"
      catenateWords="0"
      catenateNumbers="0"
      catenateAll="0"
      preserveOriginal="1"
      />
      <filter class="solr.LowerCaseFilterFactory"/>
      <filter class="solr.StopFilterFactory"/>
      <filter class="solr.PorterStemFilterFactory"/>
  </analyzer>
</fieldType>

After Doing this, you will be able to search for product name, so when you pass product name inside q="", then it will return you results. You can do the same for manufacturer, description etc...

For understanding solr, this link will be helpful. solr query in broadleaf



来源:https://stackoverflow.com/questions/22461560/rest-api-nullpointerexception-in-broadleaf-commerce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!