我是一名主要用 Java 开发的后端开发人员,所以我被教导使用 setter/getter 而不是直接访问类的属性。
现在我进入前端世界,现在进入 js/ts。我见过很多人直接访问对象变量,而不像在 Java 中那样使用 setter 和 getter,例如this.person.name.
为什么是这样?如果您不需要添加额外的代码而只是为了获取值或设置它,那么在 ts 和 js 上使用 getter/setter 有什么优势吗?
谢谢。
我正在使用 Angular 5 制作网络,每次尝试发出请求时都会收到此错误GET。我在这里阅读了大量的答案,但没有一个对我有用。
正如我所读到的,这是因为我正在向此请求添加自定义标头,这是需要完成的,因为我正在使用 Spring Security,我认为这是导致问题的原因。这是我当前的 Spring Security 配置,我通过阅读问题进行了配置,但仍然无法正常工作,我不知道我是否做错了什么:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import java.util.Arrays;
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.cors()
.and()
.authorizeRequests().anyRequest().permitAll()
.and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.csrf().disable();
}
@Override
public void configure(WebSecurity web ) throws Exception
{
web.ignoring().antMatchers( HttpMethod.OPTIONS, "/**" );
}
@Bean
CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration …Run Code Online (Sandbox Code Playgroud) 我正在开发一个 Ionic 4 应用程序。我遇到的问题是,每当用户打开键盘时,应用程序内容都会向上移动,然后当键盘关闭时,标题的一部分隐藏在手机状态栏后面。
在 Ionic 3 中,我知道可以通过修改 ion-scroll 的样式作为解决方法来解决这个问题,但在这里这不起作用。
有人可以帮我吗?
谢谢。
我想要测试的服务中有一个方法,该方法使用MongoTemplate,如下所示:
\n@Service\npublic class MongoUpdateUtilityImpl implements MongoUpdateUtility {\n private final MongoTemplate mongoTemplate;\n \n @Autowired\n MongoUpdateUtilityImpl (final MongoTemplate mongoTemplate) {\n this.mongoTemplate = mongoTemplate;\n }\n\n @Override\n public Object update(final String id, final Map<String, Object> fields, final Class<?> classType) {\n ...\n this.mongoTemplate.updateFirst(query, update, classType);\n return this.mongoTemplate.findById(id, classType);\n } \n}\nRun Code Online (Sandbox Code Playgroud)\n然后我尝试使用 mongo 模板的模拟方法来测试此方法:
\n@SpringBootTest\n@RunWith(SpringJUnit4ClassRunner.class)\n@ActiveProfiles("test")\npublic class MongoUpdateUtilityTest {\n @MockBean\n private MongoTemplate mongoTemplate;\n\n @Autowired\n private MongoUpdateUtility mongoUpdateUtility;\n\n /**\n * Test en el que se realiza una actualizaci\xc3\xb3n correctamente.\n */\n @Test\n public …Run Code Online (Sandbox Code Playgroud) 我知道对此有很多回答的问题,但没有一个对我有用。
我正在尝试进行以下调用:
this.http.options("http://...", {observe: 'response'}).subscribe((data: HttpResponse<any>) => console.log(data))
Run Code Online (Sandbox Code Playgroud)
但是,该输入的标题字段是空的!:
HttpResponse {headers: HttpHeaders, status: 200, statusText: "OK", url:
"http://...", ok: true, …}
body: ""
headers: HttpHeaders
lazyInit: ƒ ()
lazyUpdate: null
normalizedNames: Map(0)
size: (...)
__proto__: Map
[[Entries]]: Array(0)
length: 0
__proto__: Object
ok: true
status: 200
statusText: "OK"
type: 4
url: "http:/..."
__proto__: HttpResponseBase
Run Code Online (Sandbox Code Playgroud)
但是,如果我与 Postman 进行相同的调用,则会得到以下标题!
Allow ?PUT, HEAD, OPTIONS, GET
Content-Length ?0
Content-Type ?text/html; charset=utf-8
Date ?Fri, 28 Sep 2018 21:29:22 GMT
Server ?Google Frontend
X-Cloud-Trace-Context ?6627df99d34998ddeadcdcf7a2b132be;o=1
Run Code Online (Sandbox Code Playgroud)
我需要获得“允许”标题,但我似乎无法做到。 …
我想对我的实体之一进行部分更新,但如果一个属性为空,则要更新的实体也会将该值设置为空。我希望如果源中的某个属性为空,则保留源中的属性。
我已经尝试过这个但没有运气:
@Bean
public ModelMapper modelMapper() {
ModelMapper modelMapper = new ModelMapper();
modelMapper.getConfiguration().setPropertyCondition(Conditions.isNotNull());
modelMapper.createTypeMap(String.class, Date.class);
modelMapper.addConverter(new StringToDate());
modelMapper.addConverter(new DateToString());
return modelMapper;
}
Run Code Online (Sandbox Code Playgroud)
然后我像这样更新我的对象:
@Override
public void editUser(final User user) {
UserDocument userDocument = this.usersRepository.findByIdAndActivo(user.getId(), true)
.orElseThrow(UserNotFoundException::new);
userDocument = this.modelMapper.map(user, UserDocument.class);
this.usersRepository.save(userDocument);
}
Run Code Online (Sandbox Code Playgroud)
该user对象有 1 个属性设置为 null,而该对象userDocument有一个值,然后当我将它保存在数据库中时,该值消失了(因为它已转换为 null)。
有什么问题吗?
谢谢。
我正在使用带有Jersey 2.1 的Spring Boot
和 Ionic 来开发一个应用程序,我已经尝试了我找到的每一个帖子,但没有一个帖子为我解决了这个问题,我得到的错误,包括那些关于@PATCH自己创建界面注释的内容。
因此,问题是在执行PATCH请求时在浏览器上进行测试时,我在客户端收到此错误:
CORS 策略已阻止从源 ' http://localhost:8100 '访问 XMLHttpRequest :预检响应中的 Access-Control-Allow-Methods 不允许方法 PATCH。
我用于响应的过滤器如下,如果我有PATCH一个允许的方法并且它在 Postman 上完美运行,我不明白为什么我会得到这个:
筛选:
@Provider
public final class ResponseFilter implements ContainerResponseFilter {
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
MultivaluedMap<String, Object> headers = responseContext.getHeaders();
headers.putSingle("Accept-Patch", "*");
headers.putSingle("Access-Control-Allow-Origin", "*");
headers.putSingle("Access-Control-Allow-Methods", "POST, PUT, GET, OPTIONS, DELETE, PATCH");
headers.putSingle("Access-Control-Allow-Credentials", "true");
headers.putSingle("Access-Control-Max-Age", "3600");
headers.putSingle("Access-Control-Allow-Headers", "Content-Type, Accept, Authorization");
}
}
Run Code Online (Sandbox Code Playgroud)
使用的方法PATCH:
import javax.ws.rs.PATCH; …Run Code Online (Sandbox Code Playgroud) spring-boot ×4
angular ×3
cors ×2
java ×2
spring ×2
typescript ×2
cordova ×1
getter ×1
header ×1
httpclient ×1
javascript ×1
jax-rs ×1
mockito ×1
modelmapper ×1
mongodb ×1
patch ×1
preflight ×1
setter ×1