我有一个Spring Data REST应用程序,它返回这个JSON:
{
"_embedded" : {
"persons" : [ {
"personDetail" : {
"name" : "Alex",
"surname" : "Red",
"id" : {
"group" : "A",
"subclass" : "1"
},
"_links" : {
"self" : {
"href" : "https ://localhost:8080/myApp/api/personDetails/A_1"
}
}
}
}]
}
}
当我去网址时:
https:// localhost:8080 / myApp / api / personDetails / A_1
或者这个网址:
https:// localhost:8080 / myApp / api / persons / 04ee99a5-1578-400a-84be-d1ca87cda752 / personDetail
该应用程序返回此JSON:
{
"name" : "Alex",
"surname" : "Red",
"_links" : {
"self" : {
"href" : "https ://localhost:8080/myApp/api/personDetails/A_1"
},
"personDetail" : {
"href" : "https ://localhost:8080/myApp/api/personDetails/A_1"
}
}
}
" id"场似乎消失了。哪里完成了? 如何进行正确的物体投影?
这是人类:
@Entity
@Table
public class Person {
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Column(columnDefinition = "BINARY(16)", length = 16)
private UUID id;
@ManyToOne(fetch = FetchType.EAGER)
private PersonDetail personDetail;
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
public PersonDetail getPersonDetail() {
return personDetail;
}
public void setPersonDetail(PersonDetail personDetail) {
this.personDetail = personDetail;
}
}
这是PersonDetail类:
@Entity
@Table
public class PersonDetail {
@EmbeddedId
private PersonDetailId id;
@Column
private String name;
@Column
private String surname;
public PersonDetailId getId() {
return id;
}
public void setId(PersonDetailId id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
protected String[] getExcludeFieldNames() {
return null;
}
@Override
public String toString() {
return ReflectionToStringBuilder.toStringExclude(this, getExcludeFieldNames());
}
}
这是PersonDetailId类:
public class PersonDetailId implements Serializable {
@Column(name = "group", nullable = false)
private String group;
@Column(name = "subclass", nullable = false)
private String subclass;
public PersonDetailId() {
super();
}
public PersonDetailId(String group, String subclass) {
super();
this.group = group;
this.subclass = subclass;
}
public String getGroup() {
return group;
}
public void setGroup(String group) {
this.group = group;
}
public String getSubclass() {
return subclass;
}
public void setSubclass(String subclass) {
this.subclass = subclass;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append(group).append("_").append(subclass);
return builder.toString();
}
}
这是存储库REST:
@RepositoryRestResource
public interface PersonDetailRepository extends JpaRepository<PersonDetail, PersonDetailId> {
@RestResource(exported=false)
PersonDetail findBySurname(String surname);
}
这是我使用的转换器:
@Component
public class PersonDetailIdConverter implements Converter<String, PersonDetailId> {
@Autowired
private PersonDetailRepository personDetailRepository;
@Override
public PersonDetailId convert(String source) {
PersonDetailId result = null;
List<PersonDetail> details = personDetailRepository.findAll();
for (PersonDetail detail:details) {
if (detail.getId().toString().equals(source)) {
result = detail.getId();
break;
}
}
return result;
}
}
这是注册转换器的配置:
@Configuration
public class CustomizedRestMvcConfiguration extends RepositoryRestConfigurerAdapter {
@Bean
PersonDetailIdConverter personDetailIdConverter(){
return new PersonDetailIdConverter();
}
@Override
public void configureConversionService(ConfigurableConversionService conversionService) {
conversionService.addConverter(personDetailIdConverter());
super.configureConversionService(conversionService);
}
}
我使用转换器是因为它是使URL工作的唯一方法:
&#34; HREF&#34; :&#34; https:// localhost:8080 / myApp / api / personDetails / A_1&#34;
有什么想法吗? 感谢。
修改
它似乎取决于预测。 当我使用我创建的投影转到对象的链接时,返回的JSON包含我需要的所有值。
答案 0 :(得分:0)
由于this issue $sql = "insert into hotel (hotel_name, hotel_website, hotel_description, hotel_email, hotel_contact) values ('$hotel_name','$hotel_website','$hotel_desc','$hotel_email','$hotel_contact')";
mysqli_query($conn, $sql);
?>
<script type="text/javascript">
alert("Hotel Information have been Saved");
</script>
<?php
$result = mysqli_query($conn,"select hotel_id from hotel");
$row = mysqli_fetch_assoc($result);
$hotel_id = $row["hotel_id"];
echo '<script>window.location.href="property_add_address.php?id=".$hotel_id."</script>';
上有ResourceSupport.getId()
注释,因此Spring Data REST不会导出实体的ID。但是你可以重命名它们,例如到@JsonIgnore
(或者只是重命名你的getter方法)。
还要记得expose your IDs,正如Alessandro C所提到的那样。
答案 1 :(得分:0)
从Spring 3.1开始,这是注册转换器的方法:
@Import(RepositoryRestMvcConfiguration.class)
public class CustomizedRestMvcConfiguration implements RepositoryRestConfigurer {
@Override
public void configureConversionService(ConfigurableConversionService conversionService) {
conversionService.addConverter(new PersonDetailIdConverter());
super.configureConversionService(conversionService);
}
}