1

i have an jpa entity like this.

@Entity
@Table(name = "location")
@Data
public class Location {

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

    @Column(name = "LOCATION_ID", unique = true)
    @NotEmpty(message = "Please Enter Location ID")

    private String name;

    @Column(name = "LOCATION_DESCRIPTION")
    @NotEmpty(message = "Please Enter Location Description")
    private String description;

    @ManyToOne
    @NotNull(message = "Please Choose a Building")


    Building building;



    @Version
    Long version;





}

and the repository like this.

public interface LocationRepository extends PagingAndSortingRepository<Location, Long> {  
    Location findByName(@Param("name") String name);

}

i am using spring data rest i am able to create location with rest api by providing the following payload

{

"name":"adminxxxxx","description":"adminxxx" , "building": "http://localhost:8080/buildings/2"
}

now i am trying to write my custom controller which will persist the entity. this is my custom controller

@ExposesResourceFor(Location.class)
@RepositoryRestController
@BasePathAwareController
public class LocationController {

    @Autowired
    LocationRepository locationDao;

    @Autowired
    LocationResourceAssembler resourceAssembler;

    @Value("${buildings.error.messages.uniqueconstraintviolation}")
    String uniqueConstrainMessage;
    static final String TAG = LocationController.class.getSimpleName();

    @RequestMapping(value="locations",method = org.springframework.web.bind.annotation.RequestMethod.POST)
    public ResponseEntity<?> save(@RequestBody @Valid Location location) {

        try {
            location = locationDao.save(location);
            LocationResource b = resourceAssembler.toResource(location);
            return ResponseEntity.ok().body(b);
        } catch (DataIntegrityViolationException e) {

            if (locationAlreadyExists(location.getName()))
                throw new LocationAlreadyExistException(uniqueConstrainMessage, location);

            else
                throw new RuntimeException("Some Error Occured");
        }

    }

i am getting this error

exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.alamdar.model.Building: no String-argument constructor/factory method to deserialize from String value (&#39;http://localhost:8080/buildings/2&#39;)
 at [Source: java.io.PushbackInputStream@5d468b16; line: 3, column: 60] (through reference chain: com.alamdar.model.Location[&quot;building&quot;])</div></body></html>

can anyone please help?

arshid dar
  • 1,355
  • 2
  • 15
  • 23

1 Answers1

0

I am not sure why you are writing a custom controller however the issue would appear to be that you do not have a default no args constructor so Jackson cannot instantiate an instance.

This is because you are using Lombok's @Data annotation:

https://projectlombok.org/features/Data.html

You should also annotate you class with @NoArgsConstructor to have a default no-args constructor generated:

@Entity
@Table(name = "location")
@Data
@NoArgsConstructor
public class Location {

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

    @Column(name = "LOCATION_ID", unique = true)
    @NotEmpty(message = "Please Enter Location ID")

    private String name;

    @Column(name = "LOCATION_DESCRIPTION")
    @NotEmpty(message = "Please Enter Location Description")
    private String description;

    @ManyToOne
    @NotNull(message = "Please Choose a Building")
    Building building;

    @Version
    Long version;
}
Alan Hay
  • 22,665
  • 4
  • 56
  • 110