2

I can't figure out why I'm get HTTP 415 when pass POST request to http://localhost:8080/company

My JSON in POST request

{
    "id" : 7,
    "name" : "IBM"
}

Here is my method in controller

@Controller
@RequestMapping("/company")
public class CompanyController {

    @Autowired
    CompanyRepository companyRepository;

    @RequestMapping(method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Collection<Company> getAll() {
        return companyRepository.getCompanies();
    }

    @RequestMapping(method = RequestMethod.POST,
                    consumes = MediaType.APPLICATION_JSON_VALUE)
    public String add(@RequestBody Company company) {
        companyRepository.save(company);
        return "redirect:/company";
    }
}

And my entity

@Entity
@Table(name = "company")
public class Company implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    @Column
    private String name;

    @JsonIgnore
    @OneToMany(mappedBy = "company")
    @LazyCollection(LazyCollectionOption.FALSE)
    private Collection<Employee> employees;

Any ideas how to fix it?

UPD:

Response msg: The server refused this request because the request entity is in a format not supported by the requested resource for the requested method

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Arkasha
  • 117
  • 2
  • 12

1 Answers1

6

You'll notice your method is annotated with

@RequestMapping(method = RequestMethod.POST,
                consumes = MediaType.APPLICATION_JSON_VALUE)

The consumes is a restriction for the @RequestMapping saying that this method will only handle requests that have a Content-Type of application/json. Your request doesn't seem to have that header. You need to add it.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724