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 ('http://localhost:8080/buildings/2')
at [Source: java.io.PushbackInputStream@5d468b16; line: 3, column: 60] (through reference chain: com.alamdar.model.Location["building"])</div></body></html>
can anyone please help?