在Spring Data Rest 文档的这一部分中有一些不够清楚的地方:
Spring Data REST 导出器在创建输出表示之前执行任何发现的 ResourceProcessor。
对于我所注意到的,这是真的:在 RepositoryEntityController 相应方法完成后,在处理请求期间调用 ResourceProcessor。
它通过
Converter<Entity, Resource>向内部 ConversionService注册一个实例来实现这一点。
我不明白什么时候使用 this Converter<Entity,Resource>。
这是负责创建指向引用实体(例如,对象的 JSON 表示中 _links 属性下的那些对象)的链接的组件。它接受一个@Entity 并迭代它的属性,为那些由 Repository 管理的属性创建链接,并在任何嵌入的或简单的属性之间进行复制。
当然?我注意到引用实体的_links是在RepositoryEntityController. 我没有看到构建这些链接的任何其他组件:不涉及 ConversionService 或 Converter。
但是,如果您的项目需要以不同格式输出,则可以用您自己的格式完全替换默认的传出 JSON 表示。如果您在 ApplicationContext 中注册您自己的 ConversionService 并注册您自己的 Converter,那么您可以返回您选择的 Resource 实现。
我不明白怎么可能做到这一点。
我试图完全按照文档中所写的去做:我已经在 ApplicationContext 和我自己的 Converter 中注册了我自己的 ConversionService。
我已经在扩展 RepositoryRestMvcConfiguration 的自定义类中注册了 ConversionService:
@Configuration
public class RepositoryConfiguration extends RepositoryRestMvcConfiguration {
@Autowired
AuthorConverter authorConverter;
@Bean(name="conversionService")
public ConversionService getConversionService() {
DefaultFormattingConversionService conversionService = …Run Code Online (Sandbox Code Playgroud) 我有 Spring 问题,我必须使用注释加载系统属性。
我正在尝试这种方法:
@Value("${mySystemProperty}")
private String mySystemPropertyValue;
Run Code Online (Sandbox Code Playgroud)
但是当我做这个时:
System.out.println("mySystemPropertyValue="+mySystemPropertyValue);
System.out.println("system.mySystemProperty="+System.getProperty("mySystemProperty"));
Run Code Online (Sandbox Code Playgroud)
它返回:
mySystemPropertyValue=null
system.mySystemProperty=myValue
Run Code Online (Sandbox Code Playgroud)
怎么了?
谢谢
编辑
我正在尝试所有方法,但我总是为每个 System 属性返回一个空值。
我也试过:
@Autowired
private Environment environment;
Run Code Online (Sandbox Code Playgroud)
但是“环境”变量为空...
我有一个带有一些键和值的Javascript对象:
var obj = {
"key1" : "val1",
"key2" : "val2",
"key3" : "val3",
"key4" : ""
}
Run Code Online (Sandbox Code Playgroud)
我想迭代所有键并检索所有值.
我试过两种方法:
1)使用for(键中的var键)
var keys = Object.keys(obj);
for (var key in keys) {
// ...
}
Run Code Online (Sandbox Code Playgroud)
这个解决方案的问题是key对象是一个数组,所以我必须使用obj [keys [key]]].不是很漂亮
此外,检查"key4"时,返回值为"0"而不是""(空).
2)使用forEach
Object.keys(obj).forEach(function(key){
// ...
});
Run Code Online (Sandbox Code Playgroud)
这种情况下的问题是,如果我尝试这样做:
Object.keys(obj).forEach(function(key){
obj[key]; // <- obj is undefined !!
});
Run Code Online (Sandbox Code Playgroud)
"obj"变量在foreach中未定义!
迭代所有键以检索所有值的最佳方法是什么?
谢谢
我正在使用Spring Integration和调用Spring Data Rest URL 。<int-http:outbound-gateway>
我的调用是对已知资源URL(在本例中为“示例”)的简单HTTP GET。
这是配置<int-http:outbound-gateway>:
<int-http:outbound-gateway id="internalGW" request-channel="dataRestChannel"
encode-uri="true" url-expression="payload"
http-method="GET" extract-request-payload="true"
header-mapper="headerMapper">
</int-http:outbound-gateway>
Run Code Online (Sandbox Code Playgroud)
在日志中,我可以看到以下消息:
由于HTTP方法为“ GET”,因此“ extractPayload”属性与当前请求无关,并且不会为该方法发送请求主体。
我想这是指http-method="GET" extract-request-payload="true"配置,但是我不知道此警告是否与问题有关。
这是使用包含要调用的REST URL的消息来调用该出站通道:
public Object invokeUrl(String url){
MessagingChannel messagingChannel = (MessagingChannel)ApplicationContextResolver.getApplicationContext().getBean("requestChannelBean");
MessageChannel dataRestChannel = messagingChannel.getDataRestChannel();
MessagingTemplate messagingTemplate = new MessagingTemplate();
Message<?> requestMessage = MessageBuilder.withPayload(url).build();
Message<?> response = messagingTemplate.sendAndReceive(dataRestChannel, requestMessage);
return response.getPayload();
}
Run Code Online (Sandbox Code Playgroud)
调用很好,我有一个HTTP状态代码200;这是response.getPayload():
<200 OK,
{
Server=[Apache-Coyote/1.1],
Cache-Control=[no-cache,no-store,max-age=0,must-revalidate],
Pragma=[no-cache],
Expires=[0],
X-XSS-Protection=[1; mode=block],
X-Frame-Options=[DENY,DENY],
X-Content-Type-Options=[nosniff,nosniff],
Transfer-Encoding=[chunked],
Date=[Fri,22 Jul …Run Code Online (Sandbox Code Playgroud) 给出以下命令:
docker run -dit -p 9080:9080 -p 9443:9443 -p 2809:2809 -p 9043:9043 --name container_name --net=host myimage:latest bash
Run Code Online (Sandbox Code Playgroud)
如何将其转换为等效的 docker-compose.yml 文件?
我有一个覆盖方法"toPredicate"的方法:
@Override
public Predicate toPredicate(Root<Person> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
List<Predicate> predicates = new ArrayList<>();
Run Code Online (Sandbox Code Playgroud)
我必须构建谓词.简单类型很容易,例如:
@Entity
@Table
public class Person {
@Column
private String name;
@Column
private String surname;
}
Run Code Online (Sandbox Code Playgroud)
通过这个简单的课程,我可以做到:
@Override
public Predicate toPredicate(Root<Person> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
List<Predicate> predicates = new ArrayList<>();
if (StringUtils.isNotBlank(surname)) {
predicates.add(cb.equal(root.get("surname"), surname));
}
if (StringUtils.isNotBlank(name)) {
predicates.add(cb.equal(root.get("name"), name));
}
Run Code Online (Sandbox Code Playgroud)
但是如果属性是复杂类型,我如何找到复杂类型中包含的简单属性?
这是一种潜在的情况:
@Entity
@Table
public class Person {
@Column
private String name;
@Column
private String surname;
@OneToMany(fetch = FetchType.LAZY, cascade …Run Code Online (Sandbox Code Playgroud) java spring-data spring-data-jpa spring-data-rest javax.persistence
我在Spring Security中遇到这个问题。
我有一个带有SecurityConfig类的java-config实现,该实现扩展了WebSecurityConfigurerAdapter。
在此类中,我想覆盖方法“ configure()”
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true, proxyTargetClass = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter{
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
MyDaoAuthenticationProvider provider = new MyDaoAuthenticationProvider();
provider.setPasswordEncoder(passwordEncoder());
provider.setUserDetailsService(securityService);
auth.authenticationProvider(provider);
}
//...
}
Run Code Online (Sandbox Code Playgroud)
一切都OK,并且有效。
问题是“ MyDaoAuthenticationProvider”组件未在Spring Context上加载。因此,我无法在此类中注入或自动接线任何组件:
public class MyDaoAuthenticationProvider extends DaoAuthenticationProvider {
@Autowired
AuthenticationHandler authenticationHandler; // <- authenticationHandler is null, is not resolved
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
authenticationHandler.authenticate(authentication); // <- NullPointerException in this point
}
}
Run Code Online (Sandbox Code Playgroud)
这是AuthenticationHandler类:
@Component …Run Code Online (Sandbox Code Playgroud) 我正在尝试打印正在运行Maven项目构建的当前配置文件.
我正在使用它maven-antrun-plugin来在控制台上打印消息,并结合引用当前配置文件的属性.
我尝试了以下属性:
${project.activeProfiles[0].id}
${project.profiles[0].id}
Run Code Online (Sandbox Code Playgroud)
但在这两种情况下,它都会在写入时打印"字符串",而不会解析变量.
这是我的测试:
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>current active profile: ${project.activeProfiles[0].id}</echo>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Run Code Online (Sandbox Code Playgroud)
但这是我获得的结果:
main:
[echo] current active profile: ${project.activeProfiles[0].id}
Run Code Online (Sandbox Code Playgroud)
任何建议将不胜感激.
谢谢.
我有一个docker镜像和两个docker-compose.yml文件.
我想为该映像创建两个容器,每个容器都基于自己的docker-compose.yml.
我怎样才能做到这一点?
我在 Windows 10 中使用 Tomcat8.exe 时遇到了这种奇怪的情况。
我在 Windows 系统环境中定义的 JAVA_HOME 环境变量指向 JDK 1.8 路径:
JAVA_HOME=C:\Program Files\Java\jdk1.8.0_65
Run Code Online (Sandbox Code Playgroud)
当我运行“C:\Program Files\Apache Software Foundation\Tomcat 8.0\bin\Tomcat8.exe”时,服务器以另一个 JDK 启动,正如我在 catalina.log 中看到的:
12-May-2016 08:57:28.250 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version: Apache Tomcat/8.0.30
12-May-2016 08:57:28.253 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built: Dec 1 2015 22:30:46 UTC
12-May-2016 08:57:28.254 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server number: 8.0.30.0
12-May-2016 08:57:28.254 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name: Windows 8
12-May-2016 08:57:28.255 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version: 6.2
12-May-2016 08:57:28.255 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture: amd64
12-May-2016 08:57:28.255 …Run Code Online (Sandbox Code Playgroud) 我想为Oracle表计算select语句的最大值,而不是基于查询返回的所有行,而是基于这些行的子集的条件.
我将尝试用一个例子更好地解释:
ID | NAME | AGE | COMPANY
1 | Alex | 28 | A
2 | Alan | 22 | A
3 | Bob | 21 | B
4 | Carl | 20 | C
5 | Dave | 24 | C
6 | Eric | 26 | C
7 | Matt | 33 | D
Run Code Online (Sandbox Code Playgroud)
我想获得每个25岁以下公司的最大年龄,但我也想计算每家公司的总人数.所以,我想要这个结果:
COMPANY | DEPENDENTS | MAX_UNDER_25
A | 2 | 22
B | 1 | 21
C | 3 | 24
D …Run Code Online (Sandbox Code Playgroud) java ×4
spring ×4
spring-mvc ×3
docker ×2
arrays ×1
docker-run ×1
javascript ×1
json ×1
maven ×1
object ×1
oracle ×1
spring-data ×1
sql ×1
tomcat ×1
tomcat8 ×1
windows ×1
windows-10 ×1