我正在请求创建新资源的端点,并返回包含带有新创建资源的"Location"标头的201响应:
但是,当我尝试获取Angular guide中描述的标头值时,我得到一个"null"值而不是实际值.这是我的代码(注意:我正在设置responseType:'text'以避免Json解析错误,因为响应主体为空):
(...)
import { HttpClient } from '@angular/common/http';
(...)
constructor(public navCtrl: NavController, public navParams: NavParams, public http: HttpClient) {
}
ionViewDidLoad() {
let obj = {nome : "SomeName"}
this.http.post("http://localhost:8080/categorias", obj, {observe: 'response', responseType: 'text'})
.subscribe(
(resp) => {
console.log(resp.headers.get('Location'));
}
);
}
Run Code Online (Sandbox Code Playgroud)
我甚至尝试过console.log(resp.headers)来检查这个对象,它显示了一个完全不同的结构:
如何从新的HttpClient Angular API中获取HttpResponse对象的自定义标头?
我陷入了一个看似简单的问题:我想根据PUT请求中的对象id执行一些自定义验证.
@RequestMapping(value="/{id}", method=RequestMethod.PUT)
public ResponseEntity<Void> update(@Valid @RequestBody ClientDTO objDto, @PathVariable Integer id) {
Client obj = service.fromDTO(objDto);
service.update(obj);
return ResponseEntity.noContent().build();
}
Run Code Online (Sandbox Code Playgroud)
我想创建一个自定义验证器来输出自定义消息,以防我更新一些不能与我的数据库中的另一个对象相同的字段.像这样的东西:
public class ClientUpdateValidator implements ConstraintValidator<ClientUpdate, ClientDTO> {
@Autowired
private ClientRepository repo;
@Override
public void initialize(ClientInsert ann) {
}
@Override
public boolean isValid(ClientDTO objDto, ConstraintValidatorContext context) {
Client aux = repo.findByName(objDto.getName());
if (aux != null && !aux.getId().equals(objDto.getId())) {
context.disableDefaultConstraintViolation();
context.buildConstraintViolationWithTemplate("Already exists")
.addPropertyNode("name").addConstraintViolation();
return false;
}
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,对象id来自@PathVariable,而不是来自@RequestBody.我不能像上面那样调用"objDto.getId()".
另一方面,强制要求填充请求体中的对象id没有多大意义,因为这样路径变量将变得无意义.
我怎么解决这个问题?有没有办法在执行bean验证之前将PathVariable中的id注入RequestBody对象?如果没有,那么什么是可行的解决方案?谢谢.
我正在尝试使我的 Spring Boot 1.5.x REST API 项目与 2.xx 兼容,而不会破坏大量代码。我被困在我曾经使用的基于过滤器的 JWT Spring Security 实现中:
为了通过“/login”端点对凭据进行身份验证,我曾经扩展UsernamePasswordAuthenticationFilter如下:
public class JWTAuthenticationFilter extends UsernamePasswordAuthenticationFilter {
private AuthenticationManager authenticationManager;
private JWTUtil jwtUtil;
public JWTAuthenticationFilter(AuthenticationManager authenticationManager, JWTUtil jwtUtil) {
this.authenticationManager = authenticationManager;
this.jwtUtil = jwtUtil;
}
@Override
public Authentication attemptAuthentication(HttpServletRequest req,
HttpServletResponse res) throws AuthenticationException {
try {
CredenciaisDTO creds = new ObjectMapper().readValue(req.getInputStream(), CredenciaisDTO.class);
UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken(creds.getEmail(), creds.getSenha(), new ArrayList<>());
Authentication auth = authenticationManager.authenticate(authToken);
return auth;
}
catch (IOException e) {
throw new …Run Code Online (Sandbox Code Playgroud) 我正在使用一个登录端点,它返回一个不记名令牌作为响应标头,正如我在“网络”Chrome 检查窗口中看到的那样:
Response Headers
Access-Control-Allow-Credentials:true
Access-Control-Allow-Origin:http://localhost:8100
Authorization:Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJuZWxpby5jdXJzb3NAZ21haWwuY29tIiwiZXhwIjoxNTEyNzA3OTQ3fQ.pOR4WrqkaFXdwbeod1tNlDniFZXTeMXzKz9uU68rLXEWDAVRgWIphvx5F_VCsXDwimD8Q04JrxelkNgZMzBgXA
Cache-Control:no-cache, no-store, max-age=0, must-revalidate
Content-Length:188
(etc...)
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试使用 HttpClient 实例从响应中打印“标题”时:
authenticate(credentials) {
let creds = JSON.stringify(credentials);
let contentHeader = new HttpHeaders({"Content-Type": "application/json"});
this.http.post(this.LOGIN_URL, creds, { headers: contentHeader, observe: 'response'})
.subscribe(
(resp) => {
console.log("resp-ok");
console.log(resp.headers);
},
(resp) => {
console.log("resp-error");
console.log(resp);
}
);
}
Run Code Online (Sandbox Code Playgroud)
我得到了一个完全不同的结构:
HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
lazyInit : ƒ ()
lazyUpdate : null
normalizedNames : Map(0) {}
Run Code Online (Sandbox Code Playgroud)
我也尝试了 get(headerName) 方法并得到了空值。我错过了什么?如何从我的回复中获取“授权”标头?
我有一个使用 Spring Cloud OAuth2 的授权服务器 Spring Boot 项目。我将这些 bean 用于 JWT:
@Bean
public JwtAccessTokenConverter accessTokenConverter() {
JwtAccessTokenConverter tokenConverter = new JwtAccessTokenConverter();
tokenConverter.setSigningKey("my-test-jwt-secret");
return tokenConverter;
}
@Bean
public JwtTokenStore tokenStore() {
return new JwtTokenStore(accessTokenConverter());
}
Run Code Online (Sandbox Code Playgroud)
登录显然工作正常,但是当我运行该项目时,我收到此警告:
WARN 16804 --- [main] o.s.s.o.p.t.s.JwtAccessTokenConverter : Unable to create an RSA verifier from verifierKey (ignoreable if using MAC)
Run Code Online (Sandbox Code Playgroud)
我该如何摆脱这个警告?
spring-security spring-boot spring-security-oauth2 spring-cloud-security
我正在尝试使用名为"@type"的属性在TypeScript中声明一个类型,以便与我的Java API中的某些类型对应:
export interface Foo {
id : string;
name : string;
@type: string;
}
Run Code Online (Sandbox Code Playgroud)
但是,我的VS代码中出现编译器错误.
为了确保它不是JavaScript问题,我尝试let test = JSON.parse('{"@type" : "value"}')在浏览器控制台中运行正常.
那么如何声明这样一个带有以@开头的属性名的类型呢?
我习惯从以下链接访问Java SE文档,我可以从中搜索java.awt,java.util等软件包:
https://docs.oracle.com/javase/7/docs/api/
https://docs.oracle.com/javase/8/docs/api/
但是,当访问版本9的类似链接时,我可以看到一些模块结构与Java SE文档非常不同,直到版本8."旧"Java包发生了什么?
我无法使用 Java 11 和 E(fx)clipse JavaFX 插件运行 Eclipse 2018-09 (4.9)。
我运行了Help -> Install new softwareE(fx)clipse的典型过程,但在重新启动后,Eclipse 完全崩溃:我什至无法再打开 Eclipse 以获取新工作区!
当我尝试运行 Eclipse 时,出现一个对话框:
“发生错误。请参阅日志文件 C:\workspace\.metadata\.log”
我已将该日志文件的副本放在我的 Github 上。
好吧,我可以使用这个手动库设置来运行 hello world JavaFX 应用程序,但我真的很想让 E(fx)clipse 运行,以便获得 Eclipse 菜单选项和功能。那么如何解决呢?谢谢。
如果重复,请让我知道,但找不到答案。同样,这个问题似乎没有解决这个问题。
我想在同一行中声明多个变量的类型。代替
a: float
b: float
c: float
Run Code Online (Sandbox Code Playgroud)
我想使用类似
a, b, c: float
Run Code Online (Sandbox Code Playgroud)
但是我收到语法错误。正确的语法是什么?
spring-boot ×3
angular ×2
java ×2
spring ×2
eclipse ×1
http ×1
httpclient ×1
java-9 ×1
java-module ×1
javadoc ×1
javafx ×1
javafx-11 ×1
javascript ×1
json ×1
python ×1
python-3.x ×1
rest ×1
spring-mvc ×1
typescript ×1