0

I have a Hibernate project taken from here: Spring 4 MVC+Hibernate 4+MySQL+Maven integration

The problem is that the return value calls a JSP page and not displaying the returned string as a JSON.

I've tried putting:

    @RequestMapping(value = "/", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
    @ResponseStatus(HttpStatus.OK)

but still it tries calling the JSP page named by the return statement, and not displaying it in the browser (resulting in 404).

How can I change it to return a JSON?

Ran
  • 657
  • 2
  • 13
  • 29
  • 7
    You need `@ResponseBody` on the method –  Jul 11 '16 at 10:13
  • It works! Thanks! But tell me - now if I return an object - instead if "magically" turning it into JSON, my browser return 406 (unable to handle the return value). Why? It doesn't work even if I move the `ResponseBody` right after the `public` or even if I put on the entity: `@Entity @Table(name = "test") @NamedQuery(name = "TestEntity.findAll", query = "SELECT a FROM TestEntity a")` – Ran Jul 11 '16 at 10:53
  • Adding a request for `org.codehaus.jackson` in maven couses 404 – Ran Jul 11 '16 at 11:07
  • 1
    see http://stackoverflow.com/questions/16335591/spring-mvc-json-406-not-acceptable and spring mvc documentation –  Jul 11 '16 at 11:37
  • Only adding `@EnableWebMvc" did the trick! – Ran Jul 11 '16 at 13:10
  • Another quick question - I am now doing a call with `HttpURLConnection connection = (HttpURLConnection) params[0].openConnection(); connection.setRequestMethod("POST");`, and have a string to get it. The server is being called, but the String is null. Why?(Sorry I have many questions...) – Ran Jul 11 '16 at 13:35
  • I think you misunderstood how this site is working. If you have many questions, click [here](http://stackoverflow.com/questions/ask) and ask for them, this is not some kind of forum –  Jul 11 '16 at 13:43
  • Just that my questions are connected to one another... – Ran Jul 11 '16 at 13:45

0 Answers0