1

I have the below function where I am trying to return the ResponseEntity with the list of my object. I do not know what am I missing.

    @RequestMapping(method = RequestMethod.GET, path = "/test")
    public ResponseEntity<List<Book>> test(@RequestParam("param1") String param1) {
        return new ResponseEntity<List<Book>>(this.service.searchOnParam1(param1), HttpStatus.OK);   
    }

I get the below error. How do I overcome this?

WARN 37968 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation]

Book Class.

public class Book implements Serializable, Message
{

    Logger log = LogManager.getLogger(this.getClass());
    private long id;
    public long getId() {
        return this.id;
    }
    public void setId(long id) {
        this.id = id;
    }
    @MsgField(id = "COUNT")
    @JsonProperty("COUNT")
    private String  itemCount;
    @MsgField(id = "name")
    @JsonProperty("name")
    private String  name;
    @MsgField(id = "author")
    @JsonProperty("author")
    private String  author;
    @MsgField(id = "price")
    @JsonProperty("price")
    private String  price;

    public String getPriceFormatted(Locale locale){
        String returnValue = "0";
        try {
            final NumberFormat formatter = NumberFormat.getInstance(locale);
            formatter.setMaximumFractionDigits((int) 2);
            formatter.setMinimumFractionDigits((int) 2);
            formatter.setMinimumIntegerDigits(1);
            formatter.setRoundingMode(RoundingMode.HALF_DOWN);
            returnValue = formatter.format(price);
        } catch (NumberFormatException ex) {
            log.catching(ex);
        }
        return returnValue;        
    }

}
Ace
  • 700
  • 7
  • 37

2 Answers2

0

Adding the below code in the class that implements WebMVCConfigAdapter worked for me.

    @Override
    public void configureContentNegotiation(final ContentNegotiationConfigurer configurer)
    {
        configurer.ignoreAcceptHeader(true).defaultContentType(MediaType.APPLICATION_JSON_UTF8);
    }

With my application, I am always just returning JSON data.

Ace
  • 700
  • 7
  • 37
-1

Add public getters and setters and public constructor to your Book class.

Jiji
  • 103
  • 1
  • 2
  • 11