简体   繁体   English

如何在我的 angular 应用程序中调用和使用 spring REST POST API 返回没有正文的字符串?

[英]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.

相关问题 Angular如何知道要调用哪个Spring Boot REST API? - How does Angular know which Spring Boot REST API to invoke? Spring boot post rest调用“缺少所需的请求正文” - Spring boot post rest call "Required request body is missing" 春季休息服务不归身体 - Spring rest service not returning body Spring rest endpoint不返回body - Spring rest endpoint not returning body 如何在 spring 引导中发送带有 header、参数和正文的 post restful api 作为 rest 模板 - How to send post restful api with header, params, and body as a rest template in spring boot 带有JSON主体的POST REST API调用在Java的播放框架中不起作用 - POST REST API call with json body is not working in play framework with java rest api - 如何在rest api调用中发送邮递员的身体参数 - rest api - how to send body parameters of postman in rest api call 如何在Activity App Ui中使用Spring Boot Activity Rest api - How to use Spring Boot Activity Rest api in Activity App Ui 通过HTTP POST方法调用API,而无需使用主体的参数名称 - API call by HTTP POST method without using parameter name for the body 尝试使用邮递员上的 put 和 post 方法到我的 spring 休息应用程序时出现 500 内部服务器错误 - Getting 500 internal server error when trying to use the put and post methods on postman to my spring rest app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM