I'm developing a simple REST service using Spring. I've a entity and a controller to that. My problem is that I can't use Post function from browser, it's just works from terminal. The Get function works fine from browser and terminal, but Post function just works from terminal, but I must that it works from browser.
For the code below, if I navigate to:
the result is ok, all records are returned.
Get method:
@RestController
public class CityController {
...
@GetMapping(value = "/cities", produces = "application/json; charset=UTF-8")
List<City> all() {
return repository.findAll();
}
}
For the Post method, just works from terminal if I write something like:
curl -X POST localhost:8080/cities -H 'Content-type:application/json' -d '{"name":"test", "state":"test"}'
Result is ok, record is created.
But, from browser, if I tries add a new record with:
nothing happens, and no error occurs.
Post method:
@PostMapping(path = "/cities", consumes = "application/json", produces = "application/json")
City newCity(@RequestBody City city) {
return repository.save(city);
}
Entity:
@Entity
public class City {
@Id @GeneratedValue(strategy=GenerationType.AUTO) Long id;
private String name;
private String state;
public City() {
}
public City(String name, String state) {
this.name = name;
this.state = state;
}
public void setId(Long id) {
this.id = id;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
}