我的服务有问题。我试图将数据发送到数据库。但是,我收到此消息:
错误415
服务器拒绝了此请求,因为请求实体的格式不受所请求方法所请求资源的支持。
我使用Postman发送以下代码:
控制器:
package br.com.standard.controller;
import java.util.List;
import br.com.standard.bean.Client;
import br.com.standard.service.ClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ClientController {
@Autowired
ClientService clientService;
@RequestMapping(value = "/addClient", method = RequestMethod.POST, headers = "Accept=application/json")
public void addClient(@RequestBody Client client) {
clientService.addClient(client);
}
}
模型
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="client")
public class Client {
@Id
@Column(name="id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
int id;
@Column(name="name")
String nome;
public Client() {
super();
}
public Client(int id, String nome) {
super();
this.id = id;
this.nome = nome;
}
public long getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
}
我知道这可能是一个简单的问题,但我尝试了很多方法而没有成功。
提前致谢。
答案 0 :(得分:0)
两个(替代)解决方案提案:
请务必发送标题:Accept=application/json
! (与您的客户。)
...或将headers = "Accept=application/json"
更改为
consumes = org.springframework.http.MediaType.APPLICATION_JSON
(保留其他所有内容)