Java structure in the @ResponseBody custom

0

I'm doing a mapping from the database with MyBatis in Java. This is the way I am doing it and everything is working very well:

final static String GET_CONFIGURATION = "select c.codigo, c.simbolo, c.fecha, c.moneda"
            + " from pps.cuenta c"
            + " where c.cuenta_id=#{accountId}";

@Select(GET_ACCOUNT_CONFIGURATION)
    @Results(value = { @Result(property = "code", javaType = String.class, column = "codigo"),
        @Result(property = "symbol", javaType = String.class, column = "simbolo"),
        @Result(property = "date", javaType = String.class, column = "fecha"),
        @Result(property = "currency", javaType = String.class, column = "moneda")})
    public Account getAccountConfiguration(@Param("accountId") final Integer accountId);

The above is mapped to an object Account :

public class Account {

    private String code;    
    private String symbol;  
    private String date;    
    private String currency;    

    /** Getter ans Setter */
    ...

}

In the controller I am using the mapper through a service, receiving a parameter ( accountId ):

@RequestMapping(value = SecureApiResources.Account.URI_GET_ACCOUNTS_CONFIGURATION, method = RequestMethod.GET, produces = "application/json")
    public @ResponseBody AccountConfiguration getAccountConfiguration(@PathVariable(value = "accountId") Integer accountId) {   

            Account account = accountService.getAccountConfiguration(accountId);
            ...
            return account;
}

So, when I try my endpoint I get this answer:

{
  "code": "COP",
  "symbol": "$"
  "date": "dd/MM/yyyy",
  "currency": "99.999,99"
}

However, I need this structure:

{
  "currency": {
    "code": "COP",
    "symbol": "$"
  },
  "formats": {
    "date": "dd/MM/yyyy",
    "currency": "99.999,99"
  }
}

I can create a configuration object that contains two other currency and formats , but this represents that I need to create these objects and assign them the corresponding values. This does not seem to be the most correct way.

    
asked by AndreFontaine 17.03.2016 в 18:27
source

1 answer

1

Use an instance of java.util.Map . That is, for the structure you need, it would be something like:

@SuppressWarnings("serial")
@RequestMapping(value = SecureApiResources.Account.URI_GET_ACCOUNTS_CONFIGURATION,
                method = RequestMethod.GET,
                produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<?> getAccountConfiguration(
        @PathVariable(value = "accountId") Integer accountId) {

    final Account account = accountService.getAccountConfiguration(accountId);

    ⋮

    return ResponseEntity.ok(new LinkedHashMap<String, Object>() {
        {
            put("currency", new LinkedHashMap<String, Object>() {
                {
                    put("code", account.getCode());
                    put("symbol", account.getSymbol());
                }
            });
            put("formats", new LinkedHashMap<String, Object>() {
                {
                    put("date", account.getDate());
                    put("currency", account.getCurrency());
                }
            });
        }
    });

}
    
answered by 19.03.2016 / 09:51
source