cancel
Showing results for 
Search instead for 
Did you mean: 

How does the @ ResponseBody for .json via a jquery $.getJSON work

Former Member
0 Kudos

Hi , I am creating a custom function in the CategoryPageController @ResponseBody @RequestMapping(value = "/DeliveryPass/getDeliveryAddresses.json", method = RequestMethod.GET) @RequireHardLogIn public List

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

I just tried the following:

Add a "TestCategoryPageController.java" to an Accelerator AddOn I already have.

 package de.hybris.platform.storefront.controllers.pages;
 
 import de.hybris.platform.yacceleratorstorefront.controllers.pages.AbstractSearchPageController;
 import de.hybris.platform.yacceleratorstorefront.controllers.pages.CategoryPageController;
 
 import java.io.UnsupportedEncodingException;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
 import org.apache.log4j.Logger;
 import org.springframework.context.annotation.Scope;
 import org.springframework.stereotype.Controller;
 import org.springframework.ui.Model;
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.ResponseBody;
 
 
 /**
  * Controller for a category page
  */
 @Controller
 @Scope("tenant")
 @RequestMapping(value = "/**/c")
 public class TestCategoryPageController extends AbstractSearchPageController
 {
     protected static final Logger LOG = Logger.getLogger(CategoryPageController.class);
 
     private static final String CATEGORY_CODE_PATH_VARIABLE_PATTERN = "/{categoryCode:.*}";
 
     @ResponseBody
     @RequestMapping(value = CATEGORY_CODE_PATH_VARIABLE_PATTERN + "/Test.json", method = RequestMethod.GET)
     public String getJson(@PathVariable("categoryCode") final String categoryCode, final Model model,
             final HttpServletRequest request, final HttpServletResponse response) throws UnsupportedEncodingException
     {
         return "{ category: \"" + categoryCode + "\" }";
     }
 
 }

When calling the URL:

 http://electronics.local:9001/yacceleratorstorefront/electronics/en/Open-Catalogue/Cameras/Digital-C...

I get:

 "{ category: \"576\" }"

which is not completely correct yet as it's still an HTML page and not Json, but the controller works.

Check the following:

  • do you need /yacceleratorstorefront/electronics (or whatever applies to your setup) in front of the Url you posted?

  • is your controller URL pattern really correct?

Then the following things I noticed:

  • preferrably create an AddOn for your controller, do not edit CategoryPageController (at least I understand your question that way that you did)

  • why do you delivery addresses from a category page? Is it just test code?

Former Member
0 Kudos

There is a jackson mapper jar , which handles the automatic conversion of these JSON files under the particular request of @RESPONSE BODY.

Answers (0)