0

I am new to spring rest services, I am little confused. If I have a Ajax request which needs JSON response then I added @ResponseBody annotation to my method and it gives me JSON of object.

However if I have to make a restful service then even I have to do the same. Was i creating a restful for ajax?

Ramandeep S
  • 195
  • 3
  • 14

2 Answers2

0

@ResponseBody – Annotation that indicates a method return value should be bound to the web response body. (java doc see more information here)

So all it stats is that the method return type should be written straight to the HTTP response body (and not placed in a Model, or interpreted as a view name)

The presentation of the result within the body will be affected from few parameters.
I recommend reading the following section about Content Negotiation:

Spring will use is own Heuristics to figure out the requested response presentation:

The doc's summarize this in the following paragraph:

The available options are to check the file extension in the request URI, the "Accept" header, a request parameter, as well as to fall back on a default content type. By default, file extension in the request URI is checked first and the "Accept" header is checked next.

You can use @RequestMapping to specify accept header.

<mvc:annotation-driven> element has a content-negotiation-manager attribute. By default it will work with Jackson 2 in version 4, so all you need is to add the following to your classpath

<dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.3.0</version>
</dependency>

On the client side you can consume the result with Ajax or using a direct url call. The client side technology makes no different as long as it request the right content type. If you get error 406 – your content type definition is conflicting with your request

You can look at the following example here

Haim Raman
  • 10,340
  • 6
  • 39
  • 62
0

look you need to understand what is restful webservices. restful webservices is a Methodology in which all available http methods are mapped to CURD( Create Update Read Delete )

Operation   SQL     HTTP    
Create      INSERT  POST    
Read        SELECT  GET 
Update      UPDATE  PUT 
Delete      DELETE  DELETE  

so it is called restful webservice .if you are not following this strategy then its not.

Pankaj Sharma
  • 1,765
  • 1
  • 16
  • 22