Why am I getting 406 calling API that returns "long"

180 Views Asked by At

I am developing REST apis with Spring Boot and JPA. One of the endpoints consumes JSON.

@RequestMapping(
        value = "/controller/",
        method = RequestMethod.POST,
        consumes = "application/json",
        produces = "text/plain")
    public long persistController(@RequestBody DoorController doorController) {
        return 10;  
    }

I simplified the logic in this api just for debugging purposes.

Entity class DoorController looks like this: public class DoorController implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "DOOR_CTRLR_ID")
private long id;

@Column(name = "MSG_ID")
private long messageID;

@Column(name = "CTRLR_SERL_NBR")
private String controllerSerialNumber;

@Column(name = "SAP_EQUIP_NBR")
private String sapEquipNumber;

@Column(name = "CTRLR_TYP")
private String controllerType;

@Column(name = "PREV_CTRLR_SERL_NBR")
private String prevControllerSerialNumber;

Then I started the app and tested the endpoint using Swagger-ui. The request body is the following JSON:

{
  "controllerSerialNumber": "x00001",
  "controllerType": "swing",
  "prevControllerSerialNumber": "",
  "sapEquipNumber": "",
}

I did some research, it seems for older Spring, you need to explicitly add jackson dependency. But for current Spring Boot, it is already included.

No error on the server side. When I debug it and placed a break point at return 10; in the controller class, after stepping over this statement, it would redirect to sun.reflect.NativeMethodAccessorImpl in a Class File Editor and show source not found. What does this mean and why am I still getting 406?

EDIT: After some debugging, I may have found the problem. Now if I change the return type from long to string or void, it will not give me 406. None of the numeric data types work. Tried int, double.

I already set produces = "text/plain". Now the question becomes how to return long in rest API?

0

There are 0 best solutions below