Announcement Announcement Module
Collapse
No announcement yet.
@ResponseBody and Enums Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @ResponseBody and Enums

    Hi All,

    I'm currently using @ResponseBody to return some JSON data for a number of REST calls. One of the calls does return a list of enums which is also automatically converted to JSON:

    Controller
    Code:
    @Controller
    @RequestMapping("/test")
    public class RestController {
    	
    	/**
    	 * Register all binder for custom data types so these types
    	 * can be extracted from incoming requests and values assigned
    	 * to objects.
    	 * @param binder
    	 */
    	@InitBinder
    	public void initAllBinder(WebDataBinder binder) {
    		
    		// register handler for dates
    		binder.registerCustomEditor(Date.class, new DatePropertyEditor());
    		
    		// register binder for report types
    		binder.registerCustomEditor(ReportTypeEnum.class, new ReportTypePropertyEditor());
    	}
    	
    	/**
    	 * Method to return the list of report types
    	 * @param request
    	 * @param response
    	 * @return
    	 */
    	@RequestMapping(value = "/findReportTypes", method = RequestMethod.GET)
    	public @ResponseBody List<ReportTypeEnum> findReportTypes(HttpServletRequest request, HttpServletResponse response) {
    		
    		try {
    			
    			// ... return my List<ReportTypeEnum>
    		
    		} catch (RESTException e) {
    			// ... do some error handling
    		}
    		
    		return retVal;
    	}
    	
    	// ...
    }
    My Enum looks as follows:
    Code:
    public enum ReportTypeEnum {
    	
    	SUMMARY("Summary Report"),
    YEAR("Year end Report");
    	
    	// name of the report
    	private String reportFullName;
    	
    	
    	private ReportTypeEnum(String fname) {
    		this.reportFullName = fname;
    	}
    	
    	// ....
    }
    The JSON response currently only contains the ReportTypeEnum.name value. How can I have more control over what's actually being returned to the requestor? I'd like not only to return the ReportTypeEnum.name, but also the reportFullName.

    Thanks in advance,
    Daniel

  • #2
    I've just found a solution myself. I had to implement a custom serialzer...

    Serializer
    Code:
    public class ReportTypeEnumSerializer extends JsonSerializer<ReportTypeEnum> {
    	
    	private Logger log = Logger.getLogger(ReportTypeEnumSerializer.class);
    
    	@Override
    	public void serialize(ReportTypeEnum value, JsonGenerator generator,
    			SerializerProvider provider) throws IOException,
    			JsonProcessingException {
    		
    		// output the custom Json
    		generator.writeStartObject();
    		
    		// the type
    		generator.writeFieldName("type");
    		generator.writeString(value.name());
    		
    		// the full name
    		generator.writeFieldName("name");
    		generator.writeString(value.getReportFullName());
    		
    		// end tag
    		generator.writeEndObject();
    	}
    }
    and tell the Enum so via some Annotation:

    Enum
    Code:
    @JsonSerialize(using=ReportTypeEnumSerializer.class)
    public enum ReportTypeEnum {
    	
    	SUMMARY("Summary Report"),
    YEAR("Year end Report");
    	
    	// name of the report
    	private String reportFullName;
    	
    	
    	private ReportTypeEnum(String fname) {
    		this.reportFullName = fname;
    	}
    	
    	// ....
    }
    ... and that's it. :-)

    Comment

    Working...
    X