我有,我认为是一个非常简单的Spring WebSocket应用程序.但是,我正在尝试使用路径变量进行订阅以及消息映射.
我在下面发布了一个释义的例子.我希望@SendTo注释能够根据用户返回给订阅者fleetId.即POST以/fleet/MyFleet/driver/MyDriver应通知用户/fleet/MyFleet,但我没有看到这种行为.
值得注意的是订阅文字/fleet/{fleetId}作品.这是有意的吗?我错过了一些配置吗?或者这不是它的工作原理吗?
我对WebSockets或Spring项目不是很熟悉,所以提前谢谢.
Controller.java
...
@MessageMapping("/fleet/{fleetId}/driver/{driverId}")
@SendTo("/topic/fleet/{fleetId}")
public Simple simple(@DestinationVariable String fleetId, @DestinationVariable String driverId) {
return new Simple(fleetId, driverId);
}
...
Run Code Online (Sandbox Code Playgroud)
WebSocketConfig.java
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic");
config.setApplicationDestinationPrefixes("/live");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/fleet").withSockJS();
}
}
Run Code Online (Sandbox Code Playgroud)
的index.html
var socket = new SockJS('/fleet');
var stompClient = Stomp.over(socket);
stompClient.connect({}, function(frame) {
// Doesn't Work
stompClient.subscribe('/topic/fleet/MyFleet', …Run Code Online (Sandbox Code Playgroud) 使用Hibernate的Criteria,我想执行相当于:
select distinct uspscity, state from citycomplete where USPSCITY = 'HOUSTON'
Run Code Online (Sandbox Code Playgroud)
我认为做以下操作会产生我想要的结果:
ProjectionList projList = new ProjectionList();
projList.add(Projections.distinct(Projections.property("id.state")));
projList.add(Projections.distinct(Projections.property("id.uspsCity")));
criteria.setProjection(projList);
Run Code Online (Sandbox Code Playgroud)
但是,这实际上做的是执行类似的事情:
select distinct uspscity, distinct state from citycomplete where USPSCITY = 'HOUSTON'
Run Code Online (Sandbox Code Playgroud)
显然,这会引发错误.
除了不使用Criteria之外,还有解决方案吗?
谢谢,
布兰登
我有两个JPA实体,一个带有SDR导出的存储库,另一个带有Spring MVC控制器,还有一个非导出的存储库.
MVC公开实体具有对SDR管理实体的引用.请参阅下面的代码参考.
User从中检索a时,问题就出现了UserController.SDR管理的实体不会序列化,而且似乎Spring可能正在尝试在响应中使用HATEOAS引用.
这是GET一个完全填充的User外观:
{
"username": "foo@gmail.com",
"enabled": true,
"roles": [
{
"role": "ROLE_USER",
"content": [],
"links": [] // why the content and links?
}
// no places?
]
}
Run Code Online (Sandbox Code Playgroud)
如何User通过嵌入式SDR管理实体从Controller中清楚地返回实体?
Spring MVC管理
实体
@Entity
@Table(name = "users")
public class User implements Serializable {
// UID
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@JsonIgnore
private Long id;
@Column(unique = true)
@NotNull
private String username;
@Column(name = "password_hash")
@JsonIgnore
@NotNull
private String passwordHash;
@NotNull …Run Code Online (Sandbox Code Playgroud) 是否可以允许查询方法@Params是可选的,特别是在Spring Data REST的情况下?
例如,我想将一个非常相似的搜索绑定到同一个资源路径.要做到这一点,我需要以下内容:
@RestResource(path = "driver", rel = "byDriver")
List<Bar> findByDriverId(@Param("id") String id, Pageable pageable);
@RestResource(path = "driverAndSpan", rel = "byDriverAndSpan")
List<Bar> findByDriverIdAndStartTimeGreaterThanEqualAndEndTimeLessThanEqual(@Param("id") String id, @Param("start") Date start,
@Param("end") Date end, Pageable pageable);
Run Code Online (Sandbox Code Playgroud)
这给了我:
byDriver: {
href: "http://localhost:8080/foo/search/driver{?id,page,size,sort}",
},
byDriverAndSpan: {
href: "http://localhost:8080/foo/search/driverAndSpan{?id,start,end,page,size,sort}",
}
Run Code Online (Sandbox Code Playgroud)
我想要的是能够看到类似下面的路径,其中start和end是可选参数,而不是在我的存储库中定义多个方法.
byDriverAndSpan: {
href: "http://localhost:8080/foo/search/driverAndSpan{?id,*start,*end,page,size,sort}",
}
Run Code Online (Sandbox Code Playgroud)
这可能看起来像:
@RestResource(path = "driverAndSpan", rel = "byDriverAndSpan")
List<Bar> findByDriverIdAndStartTimeGreaterThanEqualAndEndTimeLessThanEqual(@Param("id") String id, @Param(value = "start", optional = true) Date start,
@Param(value = "end", optional …Run Code Online (Sandbox Code Playgroud) 我似乎无法让Eclipse通过顶级配置元素获取任何内容.
例如:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.4.7</version>
<configuration>
<container> <-- Content Assist
... <-- No Content Assist
</container>
<deployables> <-- Content Assist
... <-- No Content Assist
</deployables>
Run Code Online (Sandbox Code Playgroud)
也许我疯了,但我知道这在过去是有效的.
我启用了完整的索引,并且我重建了我的存储库索引.
这是插件实现的限制,还是环境?
我Repository在一个JPA实体上有一个Spring Data .该实体通过连接继承进行子类化.
Spring Data REST似乎在解释这个结构时遇到了问题,至少是自动解决.或者也许我误解了它的用法Inheritance.JOINED
对任何实体的任何请求都Event返回以下内容:
{
cause: null,
message: "Cannot create self link for class com.foo.event.SubEvent! No persistent entity found!"
}
Run Code Online (Sandbox Code Playgroud)
也许我要求太高了这个项目知道如何处理这个问题,但有一种变通方法,将组中的所有的我Events在相同的/events?也许甚至允许我过滤类型?
我已经离开了下面的应用程序结构的基础知识.
Event.java
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@JsonTypeInfo(use = Id.NAME, include = As.PROPERTY, property = "type")
@JsonSubTypes({
@Type(value = SubEvent.class),
...
})
...
public class Event {
@Id
private long id;
...
}
Run Code Online (Sandbox Code Playgroud)
SubEvent.java
@Entity
public class SubEvent extends Event {
private String code;
...
}
Run Code Online (Sandbox Code Playgroud)
EventRepository.java
@RepositoryRestResource(path …Run Code Online (Sandbox Code Playgroud) 让我先说明我没有直接使用Spring Cloud Config,它可以通过Spring Cloud Hystrix入门传递.
仅使用时@EnableHystrix,Spring Cloud也会尝试查找配置服务器,但由于我没有使用,因此预计会失败.据我所知,该应用程序运行正常,但问题出在状态检查中.健康显示,DOWN因为没有配置服务器.
浏览项目的源代码,我希望spring.cloud.config.enabled=false禁用此功能链,但这不是我所看到的.
升级到1.0.0.RC1(添加此属性)并使用@EnableCircuitBreaker:
{
status: "DOWN",
discovery: {
status: "DOWN",
discoveryClient: {
status: "DOWN",
error: "org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.cloud.client.discovery.DiscoveryClient] is defined"
}
},
diskSpace: {
status: "UP",
free: 358479622144,
threshold: 10485760
},
hystrix: {
status: "UP"
},
configServer: {
status: "DOWN",
error: "org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http: //localhost: 8888/bootstrap/default/master":Connection refused: connect; nested exception is java.net.ConnectException: Connection refused: connect" …Run Code Online (Sandbox Code Playgroud) 据我所知,提供了将复杂对象转换为适当的HAL格式的方法.这当然可以用于编组框架本身中的对象. Resource和Link物体等
对于一个用例的缘故:
Company 1是现有的Company在我的系统.我想添加一个Employee适合的新功能Company 1
下面是Employee您从基于Spring Data REST的服务收到的示例对象.Spring HATEOAS还提供了自己构建这些对象的方法.
{
"id": null,
"firstName": "bZWthNFk",
"lastName": "GtTnrqka",
"loginId": "zTk5rT",
"active": true,
"_links": {
"company": {
"href": "http://localhost/companies/1";
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,这似乎不适用于POST对象.据我了解,同一个对象必须被POST为:
{
"id": null,
"firstName": "bZWthNFk",
"lastName": "GtTnrqka",
"loginId": "zTk5rT",
"active": true,
"company": "http://localhost/companies/1"
}
Run Code Online (Sandbox Code Playgroud)
据我所知,HATEOAS或Data REST项目无法通过或通过其他方式生成此对象以发布到有效的基于HAL的服务RestTemplate.事实上,我找不到任何方法可以在没有手动编组的情况下轻松地发布复杂对象.假设这个我错了吗?
如何构建一个有效的Java SDK进行服务到服务通信,利用HATEOAS原则,而无需使用此工具实际POST对象?
长话短说,我想发布这个对象而不必手工序列化关联的URI.
public class Employee {
private Integer id;
@NotNull
private Company company;
private String …Run Code Online (Sandbox Code Playgroud) 我有一个奇怪的业务要求.
我们有多个不相关的实体类型需要显示在一个统一的列表中,其中包含来自实体的一些基本信息,按照它们都保证的唯一字段排序,DATE.这些实体可能甚至可能不在同一个数据库中.结果集需要是可分页的.
有没有可行的方法通过Criteria,HQL或一些理智的手段实现这一目标?
鉴于 Spring Data 和相关 REST 存储库的主要好处之一是大多数时候开发人员不必担心底层实现,是否有一种开箱即用的方式来利用 Spring Cloud Netflix 库,特别是本例中的 Hystrix 注释,无需扩展所提供的存储库接口中的每个调用或创建我自己的实现?
java ×6
spring ×4
spring-data ×4
criteria ×2
hibernate ×2
hystrix ×2
spring-boot ×2
spring-cloud ×2
distinct ×1
eclipse ×1
hql ×1
jpa ×1
m2eclipse ×1
maven ×1
netflix ×1
pagination ×1
sockjs ×1
spring-mvc ×1
sql ×1
stomp ×1
websocket ×1