我正在处理这个问题.假设我有这样的回答:
{
"id":"decaa828741611e58bcffeff819cdc9f",
"statement":"question statement",
"exercise_type":"QUESTION"
}
Run Code Online (Sandbox Code Playgroud)
然后,基于exercise_type属性,我想实例化不同的对象实例(子类ExerciseResponseDTO
),所以我创建了这个混合:
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "exercise_type")
@JsonSubTypes({
@Type(value = ExerciseChoiceResponseDTO.class, name = "CHOICE"),
@Type(value = ExerciseQuestionResponseDTO.class, name = "QUESTION")})
public abstract class ExerciseMixIn
{}
public abstract class ExerciseResponseDTO {
private String id;
private String statement;
@JsonProperty(value = "exercise_type") private String exerciseType;
// Getters and setters
}
public class ExerciseQuestionResponseDTO
extends ExerciseResponseDTO {}
public class ExerciseChoiceResponseDTO
extends ExerciseResponseDTO {}
Run Code Online (Sandbox Code Playgroud)
所以我创建ObjectMapper
如下
ObjectMapper mapper = …
Run Code Online (Sandbox Code Playgroud) 我对这些图书馆很新,我在绘制这个图时遇到了麻烦:
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
import random
df5 = pd.read_csv('../../../../datos/tiempos-exacto-variando-n-m0.csv', sep=', ', engine='python')
print(df5)
df5['n'] = df5['n'].apply(lambda x: x**2)
sns.jointplot(df5['n'], df5['tiempoTotal'], kind="reg")
sns.plt.show()
Run Code Online (Sandbox Code Playgroud)
我得到这个输出:
n m tiempoTotal
0 1 0 2274
1 2 0 3370
2 3 0 5709
3 4 0 8959
4 5 0 13354
5 6 0 18503
6 7 0 26329
7 8 0 33859
8 9 0 41110
9 10 0 52710
10 …
Run Code Online (Sandbox Code Playgroud) 这可能是一个重复的问题,但我无法弄清楚绑定碰撞在哪里.我有我的Spring Boot 1.2.6.RELEASE
服务,当我运行它时我收到此错误:
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/home/jscherman/.m2/repository/ch/qos/logback/logback-classic/1.1.3/logback-classic-1.1.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/home/jscherman/.m2/repository/org/slf4j/slf4j-log4j12/1.7.12/slf4j-log4j12-1.7.12.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
Run Code Online (Sandbox Code Playgroud)
这是mvn dependency:tree
输出
[INFO] com.myenterprise:product-manager:jar:0.0.1-SNAPSHOT
[INFO] +- com.myenterprise.product:myproject-api:jar:0.0.1-SNAPSHOT:compile
[INFO] +- com.myenterprise.product:myproject-core:jar:0.0.1-SNAPSHOT:compile
[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:1.2.6.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-starter-tomcat:jar:1.2.6.RELEASE:compile
[INFO] | | +- org.apache.tomcat.embed:tomcat-embed-core:jar:8.0.26:compile
[INFO] | | +- org.apache.tomcat.embed:tomcat-embed-el:jar:8.0.26:compile
[INFO] | | +- org.apache.tomcat.embed:tomcat-embed-logging-juli:jar:8.0.26:compile
[INFO] | | \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:8.0.26:compile
[INFO] | +- com.fasterxml.jackson.core:jackson-databind:jar:2.4.6:compile
[INFO] | …
Run Code Online (Sandbox Code Playgroud) 首先,根据Spring doc ,如果我想将用户角色映射到范围,我应该使用setCheckUserScopes(true)到DefaultOAuth2RequestFactory.所以这样做的一种方法是注入我自己的DefaultOAuth2RequestFactory bean,正如doc所说:
The AuthorizationServerEndpointsConfigurer allows you to inject a custom OAuth2RequestFactory so you can use that feature to set up a factory if you use @EnableAuthorizationServer.
Run Code Online (Sandbox Code Playgroud)
然后我做
@Configuration
@EnableAuthorizationServer
public class OAuth2AuthorizationServerConfig extends
AuthorizationServerConfigurerAdapter {
...
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints)
throws Exception {
endpoints.authenticationManager(authenticationManager)
.tokenStore(tokenStore)
.tokenServices(tokenServices());
endpoints
.getOAuth2RequestFactory(); // this doesn't return me my own DefaultOAuth2RequestFactory
}
@Bean
@Primary
public OAuth2RequestFactory defaultOAuth2RequestFactory() {
DefaultOAuth2RequestFactory defaultOAuth2RequestFactory = new DefaultOAuth2RequestFactory(
clientDetailsService);
defaultOAuth2RequestFactory.setCheckUserScopes(true);
return defaultOAuth2RequestFactory;
}
}
Run Code Online (Sandbox Code Playgroud)
我忽略了AuthorizationServerEndpointsConfigurer中的方法requestFactory().这是将它传递给Spring …
java authentication spring user-permissions spring-security-oauth2
我正在使用Spring Booot MVC 1.2.2.RELEASE.我有我的多模块项目,结构如下:
example-backend-development(parent)
---> example-backend-development-domain
---> example-backend-development-service
Run Code Online (Sandbox Code Playgroud)
我想让两个模块独立,或者至少从服务域创建域,因为服务在其类路径中具有域模块.因此,两个模块都将application.properties文件放在各自的资源文件夹中.我想这样做,因为我不想处理来自服务模块的所有属性(例如,属于域模块的Jpa属性).
例如,我在服务模块中有这个application.properties
logging.level.org.springframework.web: DEBUG
logging.level.org.hibernate: ERROR
logging.level.com.example.movies: DEBUG
Run Code Online (Sandbox Code Playgroud)
而这一个在域模块中
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/sarasa_db
spring.datasource.username=root
spring.datasource.password=mypassword
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.show-sql=true
Run Code Online (Sandbox Code Playgroud)
问题是当我启动我的服务时,因为它们都是application.properties在服务类路径中,然后它将来自服务的application.properties替换为来自域的另一个.如果它们具有相同的名称,我想合并它们,就像这种情况一样.这里的春天记录(DEBUG + INFO)
:: Spring Boot :: (v1.2.2.RELEASE)
2015-05-12 15:06:24.794 DEBUG 7728 --- [ main] o.s.w.c.s.StandardServletEnvironment : Adding [servletConfigInitParams] PropertySource with lowest search precedence
2015-05-12 15:06:24.797 DEBUG 7728 --- [ main] o.s.w.c.s.StandardServletEnvironment : Adding [servletContextInitParams] PropertySource with lowest search precedence
2015-05-12 15:06:24.798 DEBUG 7728 --- [ main] o.s.w.c.s.StandardServletEnvironment : Adding [systemProperties] PropertySource with lowest search …
Run Code Online (Sandbox Code Playgroud) 我有以下代码:
struct Foo {
int var1;
int var2;
friend std::ostream& operator<<(std::ostream& os, const Foo& s){
return os << "[Foo] " << s.var1 << "," << s.var2 ;
}
};
int main() {
Foo foo;
foo.var1 = 1;
foo.var2 = 2;
std::list<Foo> list;
list.push_back(foo);
Foo &foo2 = list.front();
foo2.var2 = 5;
std::cout << "foo (" << &foo << "): " << foo << std::endl;
std::cout << "foo2 (foo from list) (" << &list.front() << "): " << foo2 << std::endl;
} …
Run Code Online (Sandbox Code Playgroud) 我有我的Spring Boot 1.2.5.RELEASE服务,我想使用HikariCP
数据源而不是默认的tomcat-jdbc.所以,根据这个Spring Boot Reference我明白我只需tomcat-jdbc
要从类路径中排除并添加HikariCP
.
所以这是我的pom.xml:
...
<dependencyManagement>
...
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>${spring-boot.version}</version>
<exclusions>
<exclusion>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</dependency>
</dependencies>
...
Run Code Online (Sandbox Code Playgroud)
maven依赖树:
[INFO] --- maven-dependency-plugin:2.1:tree (default-cli) @ myproject-user-manage-webservice ---
[INFO] com.mybusiness.myproject:myproject-user-manage-webservice:jar:0.0.1-SNAPSHOT
[INFO] +- com.mybusiness.myproject:myproject-commons:jar:0.0.1-SNAPSHOT:compile
[INFO] | \- com.mybusiness.myproject:myproject-core:jar:0.0.1-SNAPSHOT:compile
[INFO] | \- com.mybusiness.myproject:myproject-core-commons:jar:0.0.1-SNAPSHOT:compile
[INFO] +- com.mybusiness.myproject:myproject-api:jar:0.0.1-SNAPSHOT:compile
[INFO] | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.4.4:compile (version managed …
Run Code Online (Sandbox Code Playgroud) 我知道我不能这样做:
public abstract class DTODomainTransformer<T, S> {
public abstract S transform(T);
public abstract T transform(S);
}
Run Code Online (Sandbox Code Playgroud)
因为我得到编译器投诉:
Method transform(T) has the same erasure transform(Object) as another method in type Transformer<T,S>
Run Code Online (Sandbox Code Playgroud)
据我所知,是因为这两个T
和S
可扩展同一类.所以这样做我可以告诉他"不,他们不一样,所以放轻松"
public interface Transformer<T extends AbstractDTO , S extends AbstractDomain> {
public abstract S transform(T object);
public abstract T transform(S object);
}
Run Code Online (Sandbox Code Playgroud)
然后,我的问题是,有没有办法告诉编译器T
并S
从不同的类扩展而不告诉具体哪些?我的意思是,在最后一种情况下,我已经指定了哪些类T
和S
(分别扩展).但是,如果我想要它更通用而不指定它们怎么办?我想告诉编译器,"嘿,编译器,T
并且S
不一样!它们是不同的类.我不确切地知道它们是哪个类,但我确信它们是不同的".
我正在使用Spring 4.16并且我有我的ValidationAspect,它验证方法参数并抛出ValidationException如果有问题.这是在我运行服务器并发送请求时调用,但不是在测试时调用:
package com.example.movies.domain.aspect;
...
@Aspect
public class ValidationAspect {
private final Validator validator;
public ValidationAspect(final Validator validator) {
this.validator = validator;
}
@Pointcut("execution(* com.example.movies.domain.feature..*.*(..))")
private void selectAllFeatureMethods() {
}
@Pointcut("bean(*Service)")
private void selectAllServiceBeanMethods() {
}
@Before("selectAllFeatureMethods() && selectAllServiceBeanMethods()")
public synchronized void validate(JoinPoint joinPoint) {
// Validates method arguments which are annotated with @Valid
}
}
Run Code Online (Sandbox Code Playgroud)
配置文件,我创建方面bean的方面
package com.example.movies.domain.config;
...
@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class AspectsConfiguration {
@Bean
@Description("Hibernate validator. Used to validate request's input")
public Validator validator() { …
Run Code Online (Sandbox Code Playgroud) 关于这个文档,我理解如果我有我的GroupService实现GroupManager并覆盖它的方法,那么我不能用验证约束进行注释,因为Hibernate Validator不允许它(结果证明它被称为Liskov替换原则).我的意思是做类似的事情
public class GroupService implements GroupManager{
@Override
public List<String> findUsersInGroup(@NotNull String groupName) {
...
}
}
Run Code Online (Sandbox Code Playgroud)
然后ConstraintDeclarationException
会被提出来,对吧?所以解决方案显然是将这些约束放在接口上,但在这种情况下:
GroupManager
属于Spring Security
).那怎么办呢?Hibernate Validator
迫使我"弄脏"界面java ×8
spring ×4
spring-boot ×3
c++ ×1
constraints ×1
datasource ×1
generics ×1
hikaricp ×1
jackson ×1
jpa ×1
matplotlib ×1
maven ×1
pandas ×1
python ×1
reference ×1
seaborn ×1
slf4j ×1
spring-aop ×1
spring-mvc ×1
spring-test ×1
validation ×1