1

Getting the following exception for Spring Rest controller webservices :-

org.springframework.web.client.HttpClientErrorException: 415 Unsupported Media Type at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91) at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:667)

Code:

public class PaymentProcessController {

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


    public ModelAndView savePayFailure(HttpServletRequest request, HttpServletResponse response, HttpSession session)
{
        RestTemplate restTemplate = new RestTemplate();
       Invoice invoice = restTemplate.getForObject(
                    "http://localhost:8080/rooftop/invoice/findByProperty/TRANASACTION_NO/" + tx
nid, Invoice.class);


}

The REST client is written in InvoiceRestController as below :

InvoiceRestController:

  @RestController @RequestMapping(value = "/invoice/") 

public class
            InvoiceRestController {     @RequestMapping(value =
            "/findByProperty/{property}/{value}", method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE, consumes =
            MediaType.APPLICATION_JSON_VALUE)

  public ResponseEntity<Invoice> findByProperty(@PathVariable String property, @PathVariable String value) throws Exception {

        }
Daniel
  • 2,355
  • 9
  • 23
  • 30
Aaruhi
  • 77
  • 1
  • 4
  • 13

1 Answers1

0

The issue is very much local to your set up so I guess an universal answer can't be provided. Only steps to debug the issue.

1. First make sure that Http message converters are in place i.e. a library like jackson is included in your classpath & if library is included, make sure that jar is not corrupt. Sometimes, this is the reason for error that you are getting.

This library is automatically included with Spring Boot.

If not using any frameworks, you might need to do something like this

2. Second, when you are making rest call from savePayFailure , make sure that you set & pass appropriate content negotiation headers such as Accept & Content-Type in RestTemplate.

Your rest api is very clear as what it consumes & what it produces but while making api call, you don't set any such expectations. Since, this is your custom client, you need the set up to convert the messages & headers.

How to set an “Accept:” header on Spring RestTemplate request?

HTTP get with headers using RestTemplate

Add my custom http header to Spring RestTemplate request / extend RestTemplate

You attempt both steps in that order & update question with updated code.

Sabir Khan
  • 9,826
  • 7
  • 45
  • 98
  • Thanks but the content is already updated on the points you mentioned. Here we need expertise on use of restTemplate for exchanging values as /findByProperty/{property}/{value} – Aaruhi Dec 17 '18 at 12:29
  • When you are calling `/findByProperty/{property}/{value}` , a http request is being sent to api, you need to set those headers for that resttemplate call since your jsp controller is client now.Every client ( swagger, postman etc etc ) set those headers. – Sabir Khan Dec 17 '18 at 12:44