0

I am trying to implement REST API endpoints in y application.

@Controller
 public class HerokuController {

    @RequestMapping(value = "/heroku/resources/", method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
    public  ResponseEntity<JSONObject> provision(@RequestBody JSONObject body){
        System.out.println("called! \n");
        JSONObject response = new JSONObject();
        response.put("id", 555);
        response.put("message", "Provision successful!");
          return new ResponseEntity<JSONObject>(response,HttpStatus.OK);        
    }

So I wrote this class containing a method which mapping is (heroku/ressources). But when I try to call it, I get a 404 error because /WEB-INF/heroku/resources.jsp not found. However, I don't even want to get a view but a HTTP response.

Can anyone tell me which configuration file should we generally modify to tell Spring that this controller doesn't want to send back a view but a HTTP response?

The method is however called if I change it to this :

@RequestMapping(value = "/heroku/resources/", method = RequestMethod.POST)
    public  ModelAndView provision(final HttpServletRequest request){
            System.out.println("called! \n");
            JSONObject response = new JSONObject();
            response.put("id", 555);
            response.put("message", "Provision successful!");
            final Map<String, Object> result = new HashMap<String, Object>();
          return new ModelAndView("jsonView",result);   
    }

So changing the return type to "ModelAndView".

thanks.

user3242743
  • 1,751
  • 5
  • 22
  • 32
  • http://stackoverflow.com/questions/20067057/programmatically-change-http-response-status-using-spring-3-restful – Genzotto Jul 03 '14 at 10:32
  • @Genzotto, I do not believe that will answer the OPs question. I believe what he needs to do is add the `@ResponseBody` annotation. The problem is that Spring MVC will try to "guess" which view you mean if you don't tell it. It does this by looking at the request URI, picking off the last bit (in your case "resources"), and tacking on the suffix of ".jsp". In this case, OP wants to not send back the name of a view but rather the contents of a *view*. – CodeChimp Jul 03 '14 at 11:19

2 Answers2

2

You are missing the @ResponseBody

@RequestMapping(value = "/heroku/resources/", method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody ResponseEntity<JSONObject> provision(@RequestBody JSONObject body){
    System.out.println("called! \n");
    JSONObject response = new JSONObject();
    response.put("id", 555);
    response.put("message", "Provision successful!");
      return new ResponseEntity<JSONObject>(response,HttpStatus.OK);        
}
Alex
  • 25,147
  • 6
  • 59
  • 55
  • https://www.youtube.com/watch?v=wylViAqNiRA in this tutorial, it says that when we use the "ResponseEntity" Objet, we don't use the @ResponseBody annotation. – user3242743 Jul 03 '14 at 10:10
  • And here (spring docs) they use it: http://docs.spring.io/spring-roo/reference/html/base-json.html – Alex Jul 03 '14 at 10:11
  • @user3242743 Describe what you mean by "didn't work". I can attest that using `@ResponseBody` is most definitely the way to inform Spring that you intend to return something that is not a "view". – CodeChimp Jul 03 '14 at 11:21
  • I mean that i get a : No mapping found for HTTP request with URI in DispatcherServlet with name 'appServlet'. – user3242743 Jul 03 '14 at 11:31
0

I had the same problem once, for fix that you can use @RestController instead of @controller (this will send Json by default) and you can definy your method like this

@RequestMapping(value = "/heroku/resources/", method = RequestMethod.POST)
public JsonOut provision(@RequestBody JsonIn json)

I always made my object with the value that i will get from the client, and alway the definition of the output

Ex

public class JsonOut{

    protected String id;
    protected String message;

    ...set ....get

}

and you have to put in the spring xml file this two value

<mvc:annotation-driven />

<context:annotation-config/> 

With this configuration you will have json always!

This will work with spring 4, i dont know if with spring 3 will work

nicearma
  • 750
  • 2
  • 8
  • 21