2

I have found few questions on this topic however it does not necessary answer my question

Basicaly I am passing some values via url so data can be gathered from database. I can do it via method= RequestMethod.GET however I would like to do it via POST so users doesnt see parameters in URL.

I am not sure i am using the best method, i bet there is something much advance in order to achieve this .

Cotroller class
    @RequestMapping(value="/empresa", method= RequestMethod.POST)
    public String empresa(Model model, Principal principal,    @RequestParam("get_Business_ID") String get_Business_ID){

    // get selected business
    List<Business> selectedBusiness = businessService.getBusinessByBusinessID(get_Business_ID);
    System.out.println("business selected= "+ selectedBusiness.get(0).getBusiness_name());      
    model.addAttribute("selectedBusiness",selectedBusiness);

    //Destaque semanal
    List<Business> businessList = businessService.getCurrentBusiness();
    model.addAttribute("businessList", businessList);

    return "empresa";
}

   JSP page link 
 href="${pageContext.request.contextPath}/empresa?get_Business_ID=${business.business_id}"

error Type Status Report

Message Request method 'GET' not supported

Description The method received in the request-line is known by the origin server but not supported by the target resource.

maybe RequestMethod.GET only works if i am using a form with post method?

Is there any other way to achieve this?

Thanks in advance

Vishnu T S
  • 3,476
  • 2
  • 23
  • 39
renan gado
  • 49
  • 1
  • 7

2 Answers2

2

You have annotated your method with POST

@RequestMapping(value="/empresa", method= RequestMethod.POST)

So change this to

@RequestMapping(value="/empresa", method= RequestMethod.GET)

If you want it to be a POST request try form submit instead of href

Still you need href? then try this

Make a link use POST instead of GET

Vishnu T S
  • 3,476
  • 2
  • 23
  • 39
  • I know that this is going to work but them my get request will apear in URL, I would like to use post because of security – renan gado Dec 12 '17 at 12:42
0

You are facing this issue because browser never sends anything, browser only receives requests and forwards them to your back end logic.

Difference between GET is simple fetching of data without any data from your side and POST is in information that you send with your request ( for example you send data that you need to save specific customer, I need this firstName, lastName, email etc to be saved ). With that being said, you can switch between @GetMapping or @PostMapping depending what you need for your application.

To be more precise between get and post

GET A GET method should be used to retrieve data from the server. Multiple get requests to the same URL should be valid and no data should be changed on the server side.

However, this doesn't mean it is not possible to make a GET request change things server side, but you should try to make sure you are following the standard.

POST A POST method should be used when you need to create, update or delete data on the server side. Making the same POST request multiple times may not be safe and may result in inconsistent data. The content of a POST request is sent in the request body. Hence, you don't see the parameters in your browser, but it is easy to see them if you wanted to (Even using the browser developer tools) so it is no more safe than a GET request.