Avoid annotating all methods with @ResponseBody


Gabriel Sanmartin

I am creating a REST api where I am returning domain objects as JSON entities.

So far I'm creating a controller where for each method I have to annotate it like this:

@RequestMapping(value="/entity/{id}", produces = "application/json; charset=utf-8", method=RequestMethod.GET)    
@ResponseBody
public String getEntity(@PathVariable Long id)
{
     (...)
}

Since all my methods in that controller are JSON entities, is there any way to imply to spring that I want all methods to return @ResponseBodyobjects? Also, I had to comment, produces = "application/json; charset=utf-8"because otherwise the objects returned would have the wrong encoding (and they wouldn't be returned as JSON). Can I also do this for all methods without having to annotate every method?

Michał Rybak

You can use @RequestMappingwith producesat the type (class) level - for me it works without problems:

@Controller
@RequestMapping(value = "/foo", produces = "text/plain; charset=UTF-8")
public class FooController { ... }

However, as you can see from the definition, it can only be applied at the method level in Spring 3.x.@ResponseBody

EDIT: As pointed out by @Prancer, as of v4.0 Spring supports @ResponseBodythis at the type level .

Related


Avoid annotating all methods with @ResponseBody

Gabriel Sanmartin I am creating a REST api where I am returning domain objects as JSON entities. So far I'm creating a controller where for each method I have to annotate it like this: @RequestMapping(value="/entity/{id}", produces = "application/json; charset

Avoid annotating all methods with @ResponseBody

Gabriel Sanmartin I am creating a REST api where I am returning domain objects as JSON entities. So far I am creating a controller where I have to annotate each method like this: @RequestMapping(value="/entity/{id}", produces = "application/json; charset=utf-8

Annotating unstable classes/methods for Javadoc

iceberg When developing new classes/methods for a Java project, sometimes you want people to try out your new code, but don't want to guarantee that it will be backward compatible in future releases. In this case, use something like the @Unstable annotation to

Annotating unstable classes/methods for Javadoc

iceberg When developing new classes/methods for a Java project, sometimes you want people to try out your new code, but don't want to guarantee that it will be backward compatible in future releases. In this case, use something like the @Unstable annotation to

Annotating unstable classes/methods for Javadoc

iceberg When developing new classes/methods for a Java project, sometimes you want people to try out your new code, but you don't want to guarantee that it will be backward compatible in future releases. In this case, use something like the @Unstable annotatio

Annotating unstable classes/methods for Javadoc

iceberg When developing new classes/methods for a Java project, sometimes you want people to try out your new code, but you don't want to guarantee that it will be backward compatible in future releases. In this case, use something like the @Unstable annotatio

How to avoid activating all methods in a class?

Colleze I'm trying to make an ArrayList that handles different GUI components. This class should have methods, some of which won't work for all components, so I've tried using conditional buys and can't seem to solve it that way. Can you point me in the right

How to avoid activating all methods in a class?

Colleze I'm trying to make an ArrayList that handles different GUI components. This class should have methods, some of which won't work for all components, so I've tried using conditional buys and can't seem to solve it that way. Can you point me in the right

Avoid annotating @Where clauses in models (Hibernate)

Annoirq I have in my model: @Entity @Where(clause="is_deleted <> '1'") public class Ad { In 95% of cases I will use undeleted ads. However, for one method, I need to get all the removed ads. How can I avoid Whereusing this clause in the query? I am using Crit

Avoid annotating @Where clauses in models (Hibernate)

Annoirq I have in my model: @Entity @Where(clause="is_deleted <> '1'") public class Ad { In 95% of cases I will use undeleted ads. However, for one method, I need to get all the removed ads. How can I avoid Whereusing this clause in the query? I am using Crit

Hibernate/JPA - Annotating bean methods vs fields

benstpierre: I have a simple question about Hibernate usage. I keep seeing people using JPA annotations in one of two ways, by annotating the fields of the class and also by annotating the get method of the corresponding bean. My question is the following: wit

Hibernate/JPA - Annotating bean methods vs fields

benstpierre: I have a simple question about Hibernate usage. I keep seeing people using JPA annotations in one of two ways, by annotating the fields of the class and also by annotating the get method of the corresponding bean. My question is the following: wit

Hibernate/JPA - Annotating bean methods vs fields

Benstpierre : I have a simple question about Hibernate usage. I keep seeing people using JPA annotations in one of two ways, by annotating the fields of the class and also by annotating the get method of the corresponding bean. My question is the following: wi

Postman responseBody JSON get all users

crush I'm trying to create a runner in postman, but first I need to get all usernames from some JSON, but I'm having trouble getting a list of all usernames from the following JSON. var data = JSON.parse(responseBody); $.each(data.users, function

Avoid specific methods of CamelCasePropertyNamesContractResolver

Ahmedalipo Lodge I have web api controllers and am using this configuration method in the WebApiConfig file to camel case the results of all the controllers. var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter; json.SerializerSettings.Contrac

Avoid specific methods of CamelCasePropertyNamesContractResolver

Ahmedalipo Lodge I have web api controllers and am using this configuration method in the WebApiConfig file to camel case the results of all the controllers. var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter; json.SerializerSettings.Contrac