Reputation: 1424
I am using AngualrJS and Spring MVC3.2. I am trying to post simple object shown below to the server but I am getting '415 Unspported Media Type error'.
@Entity
@Table(name="Question")
public class Question implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private long id = 0;
private String statement;
private int frequency;
private int difficulty;
private String comment;
private String reference;
@Temporal(TemporalType.TIMESTAMP)
protected Date regTime;
@Temporal(TemporalType.TIMESTAMP)
protected Date updTime;
@OneToMany(mappedBy="question", fetch=FetchType.EAGER)
@NotFound(action=NotFoundAction.IGNORE)
private List<Answer> answers = new ArrayList<Answer>();
//Getters and setters
}
@Entity
@Table(name="Answer")
public class Answer implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private long id = 0;
private String answer;
@Column(name="correct", columnDefinition="INT(1)")
private boolean correct;
@ManyToOne
@JoinColumn(name="questionId", referencedColumnName="id")
@NotFound(action=NotFoundAction.IGNORE)
@JsonIgnore
private Question question;
//Getters and setters
}
@Controller
@RequestMapping("/question")
public class QuestionController {
@RequestMapping(method=RequestMethod.POST)
public @ResponseBody HttpResult submit(@RequestBody Question question) {
HttpResult result = new HttpResult();
//Do something here
return result;
}
}
services.js
$scope.submit = function(entity){
$scope.formshow = false;
var obj = angular.copy(entity);
Question.save(obj, function(data){
if (data.ok == true){
Question.add('success', 'Data has been saved successfully.');
$scope.loadData();
} else {
Question.add('danger', data.msg);
}
});
};
JSON in the JSP page
{
"id":0,
"answers":[
{
"id":0,
"answer":"a",
"correct":false
},
{
"id":0,
"answer":"b",
"correct":true
},
{}
],
"statement":"test question",
"frequency":0,
"difficulty":0,
"comment":"comment",
"reference":"ref"
}
Http header in firebug
Response Headers
Content-Length 1048
Content-Type text/html;charset=utf-8
Date Mon, 05 May 2014 12:29:56 GMT
Server Apache-Coyote/1.1
Request Headers
Accept application/json, text/plain, */*
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
I know that 415 error means that http header content-type is wrong or request data format is invalid. I have tried to change http header type forcefully but I could not change it. How do I fix this? Your answer would be appreciated.
Upvotes: 0
Views: 1563
Reputation: 981
Indeed the default Content-Type is application/json, however you're sending text/html. Nothing new to you so far, I know.
Since version 1.1.1 (if I'm not mistaken) you can enforce the content type at the $resource definition.
Can you try to define your $resource like this:
var Question = $resource('your/resource/url',{your params if any, otherwise send this as an empty object},{
post: {
method:'POST',
isArray: false,
headers: {'Content-Type': 'application/json;charset=UTF-8'}
}
} )
then, instead of Question.save(), use Question.post(...). I have created the post method just so you don't lose the save() default behavior...but you could configure the save method exactly like I've configured the post method.
P.S.: This code is untested.
Upvotes: 1