2017-04-25 1 views
0

나는 이와 같은 jpa 엔티티를 가지고있다.봄 데이터 나머지에 사용자 지정 컨트롤러를 사용하여 URI를 엔터티로 변환 하시겠습니까?

@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; 





} 

및 이와 같은 저장소. 내가 봄의 데이터 나머지를 사용하고

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

} 

내가 지금 내가 엔티티를 지속됩니다 내 사용자 정의 컨트롤러를 작성하려고하고 다음 페이로드를

{ 

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

을 제공함으로써 나머지 API를 사용하여 위치를 만들 수 있어요. 이것은 내가이 오류

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: [email protected]; line: 3, column: 60] (through reference chain: com.alamdar.model.Location[&quot;building&quot;])</div></body></html> 

사람이 도와주세요 수

을 얻고있다

@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"); 
     } 

    } 

내 사용자 정의 컨트롤러?

+0

이 HTTP : 인자없는 생성자가 생성되지 기본을 가지고 또한 @NoArgsConstructor 당신 클래스에 주석을해야

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

:

당신은 롬복의 @Data 주석을 사용하고 있기 때문에 : //stackoverflow.com/q/41324078/5380322 help;) – Cepr0

+0

저장 작업을 수행 할 사용자 지정 컨트롤러를 작성하는 이유는 무엇입니까? –

+0

이유는 내가 사용자 지정 컨트롤러를 작성 오전 coz 내가 일부 사용자 지정 논리를 쓰기 전에 엔터티를 유지해야합니다. – user2768310

답변

0

왜 사용자 지정 컨트롤러를 쓰는지 모르지만 기본 no args 생성자가 없으므로 Jackson이 인스턴스를 인스턴스화 할 수없는 것으로 보입니다.

@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; 
} 
관련 문제