0

I have object that have attributes id, name, description.

In some case i want to return whole object but in some i want to return only id, suppose when I insert new record it should only return id that is newly generated.

public class DTO { private Integer id; private String name; private String description; // getter-setter } 

controller:

@RequestMapping(value = "/{cname}/met", method = RequestMethod.POST) @ResponseBody public DTO addMet(@PathVariable String cname, @RequestBody DTO met) { return service.addMet(met); // service return whole DTO object that is newly added with new Id } 

I want to return only id as response, so any help or guidance on it that how to do so ?

Thank you.

3
  • I didn't understand it. The method addMet cannot return Integer instead of DTO? Commented Feb 25, 2015 at 2:10
  • @MarceloKeiti : I want to return response as Json like {id:'1'}, if i ll return it as integer it will just give me id in response. Commented Feb 25, 2015 at 2:11
  • Ok, so return a String and format the DTO instance using some framework like Gson. Commented Feb 25, 2015 at 2:13

4 Answers 4

0

Unless I'm missing something, you could change the return type of the method and return the id with something like

public Integer addMet(@PathVariable String cname, @RequestBody DTO met) { return service.addMet(met).getId(); } 

It might be a good idea to log the full return (assuming you have overridden toString())

public Integer addMet(@PathVariable String cname, @RequestBody DTO met) { LOG.debug("Adding: %s", met); DTO r = service.addMet(met); LOG.debug("Result: %s", r); return r.getId(); } 

As for your custom class, you could write an inner class like

static class Identity { public Identity(int id) { this.id = id; } private final int id; public int getId() { return id; } public String toString() { return String.format("id: %d", id); } } 

and return that

public Identity addMet(@PathVariable String cname, @RequestBody DTO met) { LOG.debug("Adding: %s", met); DTO r = service.addMet(met); LOG.debug("Result: %s", r); return new Identity(r.getId()); } 
Sign up to request clarification or add additional context in comments.

1 Comment

I want to return response as Json like {id:'1'}, if i ll return it as integer it will just give me id in response.
0

Did you try the JsonIgnore annotation. http://jackson.codehaus.org/1.0.1/javadoc/org/codehaus/jackson/annotate/JsonIgnore.html

2 Comments

Yes I know this already. But in some situation I want to return whole object and in some I want to just return Id. So it should be kind of dynamic.
maybe this will help then, stackoverflow.com/questions/8179986/… The blog pointed out even has a nice example, I had used it some time back when I was learning.
0

In your controller, set the fields you want to exclude to null.

Also configure Spring's underlying Jackson ObjectMapper instance to not serialize null fields:

@Configuration public class ServiceContext extends WebMvcConfigurationSupport { @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { MappingJackson2HttpMessageConverter converter = this.getConverter(); converters.add(converter); } @Bean public MappingJackson2HttpMessageConverter getConverter() { MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(); ObjectMapper mapper = this.getObjectMapper(); converter.setObjectMapper(mapper); return converter; } @Bean public ObjectMapper getObjectMapper() { JsonFactory factory = new JsonFactory(); ObjectMapper mapper = new ObjectMapper(factory); mapper.setSerializationInclusion(Include.NON_NULL); // don't serialize nulls mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); return factory; } } 

Comments

0

You need to use the @JsonView

JSON Views

It can sometimes be useful to filter contextually objects serialized to the HTTP response body. In order to provide such capabilities, Spring MVC now has builtin support for Jackson’s Serialization Views.

Here is a well explained example

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.