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.

Java Curious ღ
  • 3,622
  • 8
  • 39
  • 63

4 Answers4

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());
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

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

happyHelper
  • 119
  • 8
  • 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. – Java Curious ღ Feb 25 '15 at 02:16
  • maybe this will help then, http://stackoverflow.com/questions/8179986/jackson-change-jsonignore-dynamically The blog pointed out even has a nice example, I had used it some time back when I was learning. – happyHelper Feb 25 '15 at 02:19
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;
    }
}
fps
  • 33,623
  • 8
  • 55
  • 110
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

iamiddy
  • 3,015
  • 3
  • 30
  • 33