所以,我对spring和java一般都很新
我尝试做的是在相同的渲染视图上使用表单发布数据以过滤表单下显示的结果列表.
我有一个简单的域类如下:
@Entity
@Table(name = "SEC_PERSON")
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_SEC_PERSON")
@SequenceGenerator(name = "SEQ_SEC_PERSON", sequenceName = "SEQ_SEC_PERSON")
@Column(name = "ID")
private Long Id;
@Column(name = "CODE", nullable = false)
private String code;
@Column(name = "FIRSTNAME", nullable = false)
private String firstname;
@Column(name = "SURNAME", nullable = false)
private String surname;
@Column(name = "CREATIONDATE")
private DateTime creationDate;
//getters and setters
Run Code Online (Sandbox Code Playgroud)
一个DTO,因为我希望我的域名与我的演示文稿分离
public class PersonDTO {
private Long id;
@NotEmpty
private String code;
@NotEmpty
private …Run Code Online (Sandbox Code Playgroud) 我有一个服务器通过spring-data-rest公开资源,据我所知HAL或HATEOAS.但是当我尝试将它与Feign结合使用时,我似乎无法注册被拾取的Jackson2HalModule.
有什么东西要把Feign"客户端"连接到新的转换器吗?它使用的是另一个ObjectMapper吗?
码:
@Inject
public void configureObjectMapper(ObjectMapper mapper, RestTemplate template) {
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.registerModule(new Jackson2HalModule());
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setSupportedMediaTypes(MediaType.parseMediaTypes("application/hal+json"));
converter.setObjectMapper(mapper);
template.getMessageConverters().add(converter);
}
Run Code Online (Sandbox Code Playgroud)
服务器响应:
{
"_links" : {
"self" : {
"href" : "http://localhost:13372/user{?page,size,sort}",
"templated" : true
},
"search" : {
"href" : "http://localhost:13372/user/search"
}
},
"_embedded" : {
"user" : [ {
"id" : "5567613e5da543dba4201950",
"version" : 0,
"created" : "2015-05-28T18:41:02.685Z",
"createdBy" : "system test",
"edited" : "2015-05-28T18:41:02.713Z",
"editedBy" : "system test",
"username" : "devuser",
"email" : "dev@test.com", …Run Code Online (Sandbox Code Playgroud) 我想在Spring Data REST Repository中创建一个资源链接.我知道我们可以使用ControllerLinkBuilder.linkTo方法创建到MVC控制器的链接.据我所知,Spring Data REST从我们的Repository接口创建了MVC控制器.但是,如果我使用
Instance createdInstance = instanceRepository.save(instance);
Link link = linkTo(InstanceRepository.class).slash(createdInstance.getId()).withSelfRel();
Run Code Online (Sandbox Code Playgroud)
创建链接,我只是得到http://localhost:8080/2(没有存储库路径).如果我@RepositoryRestResource在存储库中明确指定路径,则不会发生任何变化.
当然我可以明确地创建链接,但我不想重复自己.
public interface InstanceRepository extends CrudRepository<Instance, Long> {
}
Run Code Online (Sandbox Code Playgroud)
关于如何解决这个问题而不必违反DRY原则的任何建议?
我们目前正在最新的项目中实现API库.我们正在尝试将Spring HATEOAS与HAL一起用作生成json hal响应的适当库.
使用Spring boot 1.2.5.RELEASE +提供的Spring HATEOAS版本.目前,我们正在将要返回的实际JPA实体映射为Resources.
即我们的实体看起来像这样:
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@Entity
@Table(name = "users")
public class User {
@Id
private UUID id;
@Column(nullable = false, length = 70)
private String firstName;
@Column(nullable = false, length = 70)
private String lastName;
}
Run Code Online (Sandbox Code Playgroud)
我们的仓库:
public interface UserRepository extends PagingAndSortingRepository<User, UUID>, JpaSpecificationExecutor<User> {
User findByUsername(String username);
}
Run Code Online (Sandbox Code Playgroud)
我们的服务:
@Service
@Transactional
public class UserService implements UserDetailsService {
private UserRepository userRepository;
@Autowired
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
} …Run Code Online (Sandbox Code Playgroud) 有没有办法使用@Projection接口作为SDR中资源的默认表示?通过SDR存储库还是通过自定义控制器?
过去可以通过注入ProjectionFactory和使用该createProjection方法在自定义控制器中执行此操作,但最近的Spring Data Rest更新已经破坏了这一点.
我想对一个实体强制执行一个特定的视图,SDR投影似乎是一个理想的方法,特别是在HAL API的上下文中,而不是为自定义控制器编写硬DTO类和它们之间的映射等.摘录预测不是我所追求的,因为这些只适用于查看相关资源.
我正在使用spring-hateoas:0.18.0.RELEASE和spring-boot:1.2.5.RELEASE
为了调用我的 Web 服务并通过HAL链接,我使用Traverson 客户端 (客户端服务遍历的 API,受 Traverson JavaScript 库启发)
使用Hypermedia和HateoasRest的新功能
我的问题是什么时候需要使用PagedResources和Resource?
我在这里找到的示例 Traverson 客户端示例:
final PagedResources<Resource<Customer>> resources = traverson
.follow("customers","search","findByFirstName")
.withTemplateParameters(parameters)
.toObject(new TypeReferences.PagedResourcesType<Resource<Customer>>(){});
Run Code Online (Sandbox Code Playgroud)
我写的代码是:
ParameterizedTypeReference<Resource<ProjectJSON>> resourceParameterizedTypeReference = new
ParameterizedTypeReference<Resource<ProjectJSON>>() {};
Resource<ProjectJSON> projectJSONResource = traverson
.follow("projects")
.follow("$._embedded.projects[0]._links.self.href")
.toObject(resourceParameterizedTypeReference);
Run Code Online (Sandbox Code Playgroud)
我知道这不是一回事,但是在调用Traverson.toObject()方法时,资源的最佳实践是什么?
我正在使用Spring Data REST 2.5.1,Jackson 2.8.0,Spring Boot 1.3.6.
我正试图通过RestTemplate从我的存储库中检索一个简单的实体列表.我可以在浏览器中找到终点,并获得预期的HAL数据.检索单个实体可以正常工作,如下所示.这些都使用默认的SDR端点(例如localhost:{port}/myEntity).
ResponseEntity<Resource<MyEntity>> responseEntity =
new RestTemplate()
.exchange(
uri + "/1",
HttpMethod.GET,
HttpEntity.EMPTY,
new ParameterizedTypeReference<Resource<MyEntity>>() {}, port
)
Run Code Online (Sandbox Code Playgroud)
或者新的RestTemplate().getForEntity(uri +"/ 1",MyEntity.class,port)
正如许多SO问题似乎表明的那样,找到检索列表的示例是一个问题.我试着ParameterizedTypeReference用Resources,Resource,MyEntity,数组列表.一切都没有运气.
ResponseEntity<Resources<Resource<MyEntity>>> responseEntity =
new RestTemplate()
.exchange(
uri,
HttpMethod.GET,
HttpEntity.EMPTY,
new ParameterizedTypeReference<Resources<Resource<MyEntity>>>() {}
, port
)
Run Code Online (Sandbox Code Playgroud)
当所谓的像上面几乎任何种类的Resources,Resource,List<MyEntity>,MyEntity,等等,ResponseEntity是空的.喜欢:
<200 OK,Resources { content: [], links: [] },{Server=[Apache-Coyote/1.1], Content-Type=[application/json;charset=UTF-8], Transfer-Encoding=[chunked], Date=[...]}>
Run Code Online (Sandbox Code Playgroud)
字符串JSON在浏览器中如下所示.
{
"_embedded" : {
"myEntities" : …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个示例应用程序,Spring hateos并且遇到以下异常:
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/hetos-medias]]
at java.util.concurrent.FutureTask.report(FutureTask.java:122)
at java.util.concurrent.FutureTask.get(FutureTask.java:192)
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1123)
at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:816)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1575)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1565)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/hetos-medias]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
... 6 more
Caused by: java.lang.NoSuchMethodError: org.apache.tomcat.util.res.StringManager.getManager(Ljava/lang/Class;)Lorg/apache/tomcat/util/res/StringManager;
at org.apache.tomcat.websocket.WsWebSocketContainer.<clinit>(WsWebSocketContainer.java:77)
at org.apache.tomcat.websocket.server.WsSci.init(WsSci.java:131)
at org.apache.tomcat.websocket.server.WsSci.onStartup(WsSci.java:47)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5481)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 6 more
Run Code Online (Sandbox Code Playgroud)
看起来可能有重复的jar用于类,org.apache.tomcat.util.res.StringManager.getManager 但我在类路径中找不到任何重复的jar。我正在使用tomcat 7,以下是我pom.xml和图书馆的详细信息。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
<version>1.4.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<version>1.4.0.RELEASE</version> …Run Code Online (Sandbox Code Playgroud) 我最近尝试使用Spring Boot 1.4.1和Spring Cloud Camden SR1 从这个视频和这个github repo复制Spring云示例,并遇到了一个问题.当客户端服务使用Feign客户端调用生产者服务时,反序列化的资源没有内容或链接.
进一步挖掘后,看起来这个问题与MappingJackson2HttpMessageConverter和它的Jackson ObjectMapper有关.当我查看假装客户端的SpringDecoder使用的HttpMessageConverters时,我看到2个MappingJackson2HttpMessageConverters,但转换器的ObjectMappers都没有注册Jackson2HalModule.
Spring数据休息预约服务应用程序类:
@EnableDiscoveryClient
@SpringBootApplication
public class ReservationServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ReservationServiceApplication.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
预订课程:
@Entity
public class Reservation extends BaseEntity {
private String reservationName;
public Reservation(String reservationName) {
this.reservationName = reservationName;
}
protected Reservation() {}
public String getReservationName() {
return reservationName;
}
}
Run Code Online (Sandbox Code Playgroud)
BaseEntity类具有ID和版本.
ReservationRepository类:
@RepositoryRestResource
public …Run Code Online (Sandbox Code Playgroud) spring spring-mvc spring-data-rest spring-hateoas spring-boot
我正在使用Spring Hateoas来处理HTTP响应中的HAL标准.我的控制器中有一个HTTP DELETE方法,它不返回任何内容(void).在同一实体的响应中,我想提供一个删除资源的链接.我尝试使用以下代码,但它给出了错误
无法解析方法linkTo(void)
resource.add(linkTo(
methodOn(DokumenteController.class)
.loeschenEinDokument(filenetDokumentZuordnung.getDokumentId()))
.withRel("download"));
Run Code Online (Sandbox Code Playgroud)
有没有办法我可以添加一个返回void的方法的链接?
spring-hateoas ×10
spring ×5
spring-mvc ×4
java ×3
spring-boot ×3
hal ×2
hateoas ×2
hypermedia ×1
jackson ×1
json ×1
maven ×1
rest ×1
spring-data ×1
tomcat ×1