0

I have next ajax request

$.ajax({
    contentType: 'application/json',
    mimeType: 'application/json',
    type: frm.attr('method'),
    url: frm.attr('action'),
    dataType: 'json',
    data: JSON.stringify(data),
    success: function (response) {
        alert("success" + response);
    },
    error: function (jqXHR) {
        var errorMessage = $(jqXHR.responseText).filter('p:eq(1)').find('u').text();
        alert("error " + errorMessage);
    }
});

where I transmit the letter entity

@Entity
@Table(name = "LETTERS")
public class Letter {
    @Id
    @Column(name = "ID")
    @GeneratedValue
    private long id;

    @Column(name = "MESSAGE")
    private String message;

    @Column(name = "EMAIL")
    private String email;

    @Column(name = "CREATING_DATE")
    private Date creatingDate;

    @Column(name = "DELIVERY_DATE")
    private Date deliveringDate;

    public Letter() {
    }
    //... getters and setters
}

and in my controller I can't get this entity cause unsupported media type

@RequestMapping(value = "/main", method = RequestMethod.POST)
@ResponseBody
public void addLetterToDB(@RequestBody Letter letter) {
    System.out.println(letter);
}

But if I change in method signature Letter type to String type it will works, but I wanna take the entity) Pls help.

sanastasiadis
  • 1,182
  • 1
  • 15
  • 23
jenius
  • 257
  • 1
  • 4
  • 17
  • Found answer just add dependecies for new spring version 4.1.7 http://stackoverflow.com/questions/7462202/spring-json-request-getting-406-not-acceptable – jenius Jul 20 '15 at 09:20

1 Answers1

0

Use the @Produces annotation and put there application/json. You also need a mechanism to convert the result (Letter) to JSON. Use Jackson or Gson.

ACV
  • 9,964
  • 5
  • 76
  • 81