[英]How to call and use the spring REST POST API in my angular app which is returning string without body?
I am trying to access the POST API from my spring app to angular but little bit confused how to use and access the given API in my angular app.我正在尝试从我的 spring 应用程序访问 POST API 到 angular 但有点困惑如何在我的 angular 应用程序中使用和访问给定的 API。
Spring REST API Spring REST API
@RequestMapping(value = "/getWelcomeMessage", method = RequestMethod.POST)
public String getLoginWelcomeMessage() {
return details.getLoginWelcomeMessage();
}
The given API is fetching the welcome message details from my oracle DB and returning a string value.给定的 API 从我的 oracle 数据库中获取欢迎消息详细信息并返回一个字符串值。 I am trying to access the given REST API in my angular code through services.我正在尝试通过服务在我的 angular 代码中访问给定的 REST API。 I had define the post service as follows我定义了邮政服务如下
export class LoginService {
constructor(private http : HttpClient) { }
welcomeMessageService(){
const headers = {'content-type':'application/text'}
return this.http.put("http://localhost:8080/API/getWelcomeMessage",null,
{'headers':headers});
}
}
As the post method requires three arguments URL, Body and header. But in my case my spring REST API doesn't contain any body and returning a string.由于 post 方法需要三个 arguments URL、Body 和 header。但在我的例子中,我的 spring REST API 不包含任何正文并返回字符串。 So, I had define the body as null and change the header type to text as it is JASON by default.因此,我将正文定义为 null 并将 header 类型更改为文本,因为默认情况下它是 JASON。
At last, I am trying to access the given service method by injecting it in my component as follows-最后,我试图通过将它注入我的组件来访问给定的服务方法,如下所示 -
export class LoginComponent implements OnInit {
message:string;
constructor(private loginService : LoginService) { }
ngOnInit(): void {
this.loginService.welcomeMessageService().subscribe(
response =>{
console.log(response);
this.message = response;
}
)
}
}
But when I am trying to assign the response to the string I am getting the error that string cannot be assigned to the object. I am little bit confused why this error is occurring as I had also changed the header type to string while defining my service but still getting the error.但是当我尝试将响应分配给字符串时,我收到错误消息,即无法将字符串分配给 object。我有点困惑为什么会出现此错误,因为我在定义服务时也将 header 类型更改为字符串但仍然出现错误。
It can be a great help if anybody guide me regarding this as I am new to angular and little bit confused with integration part of API with angular.如果有人在这方面指导我,那将是一个很大的帮助,因为我是 angular 的新手,并且有点混淆 API 与 angular 的集成部分。
Use { responseType: 'text' }
and also send an empty body not null使用{ responseType: 'text' }
并发送一个空体而不是 null
export class LoginService {
constructor(private http : HttpClient) { }
welcomeMessageService(){
return this.http.put("http://localhost:8080/API/getWelcomeMessage",{},
{ responseType: 'text' });
}
}
Maybe you have copied the function wrong but check also here也许您将 function 复制错了,但也请检查此处
@RequestMapping(value = "/getWelcomeMessage", method = RequestMethod.POST)
public String getLoginWelcomeMessage() {
return details.getLoginWelcomeMessage();
}
This is a Post method not a put that you are trying to call这是一个 Post 方法,而不是您要调用的 put
As for cors error add the following to the backend just above @Controller
or @RestControler
whatever you have至于 cors 错误,无论你有什么,都将以下内容添加到@Controller
或@RestControler
上方的后端
@CrossOrigin(value = {"http://localhost:4200"}, methods = {GET,POST,PUT,DELETE})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.