我无法通过我自己的角度客户端(在本地主机)使用我自己的Java Rest资源(在本地主机)。
我可以很好地通过浏览器(在wildfly服务器上运行)访问我的Java Rest资源:http://localhost:8080/restoverflow/restoverflow/resource。
我也可以很好地访问我的角度客户端(在angularCLI上运行):http://localhost:4200/。
我也可以显示来自https://jsonplaceholder.typicode.com/posts的第三方来源的rest json,该角度客户端可以正常使用,但不能显示我自己的java rest资源。
Java
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPath("/restoverflow")
public class App extends Application {
}
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/resource")
public class Resource {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Pojo getPojo() {
Pojo pojo = new Pojo();
pojo.setName("pojoName");
return pojo;
}
}
角度
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class PojosService {
constructor(private http: HttpClient) { }
getPojos(){
//my java source restoverflow doesn't work
return this.http.get('http://localhost:8080/restoverflow/restoverflow/resource');
//something else does work
//return this.http.get('https://jsonplaceholder.typicode.com/posts');
}
export class PojosComponent implements OnInit {
result: any;
constructor(private pojosService: PojosService) { }
ngOnInit() {
this.pojosService.getPojos().subscribe((data: any) => {
this.result = data;
});
}
}
pojos.component.html
<p>
pojos works! but {{ result }} does not
</p>
实际结果在本地主机:4200
pojos works! but does not
预期结果
pojos works! but [object Object] does not