【问题标题】:Restful webservice not able to read the parameters of Angular 5 service call in jersyRestful Web 服务无法读取球衣中 Angular 5 服务调用的参数
【发布时间】:2018-04-06 05:07:10
【问题描述】:

我正在尝试通过从 httpParams 和标头中传递参数来从 Angular 组件调用 Restful Webservice,但是 Jery Restful Webservice 无法从请求中读取用户名(uname)。

Angular 5 中的 AppTestService

import { Injectable, Inject } from "@angular/core";
import { Http, Headers } from "@angular/http";
import { HttpParams, HttpHeaders } from "@angular/common/http"
import { HttpClient } from "selenium-webdriver/http";

@Injectable()
export class AppTestService {
    constructor(@Inject(Http) private proxy: Http){}

    url = "http://localhost:8081/loginangular/webapi/AngularUser/login";
    postGitUser(){        
        let headers = new Headers();
        headers.append('Content-Type','application/json; charset=utf-8');

        return this.proxy.post(this.url,{params:{uname: 'rajdharg',paswd: '****'}}, {headers:headers}).subscribe(resp => {
            console.log("response %o , ", resp);
        });
    }
}

RestFul Webservice 中的 pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.grajdhar</groupId>
    <artifactId>loginangular</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>loginangular</name>

    <build>
        <finalName>loginangular</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <inherited>true</inherited>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.glassfish.jersey</groupId>
                <artifactId>jersey-bom</artifactId>
                <version>${jersey.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet-core</artifactId>
            <!-- use the following artifactId if you don't need servlet 2.x compatibility -->
            <!-- artifactId>jersey-container-servlet</artifactId -->
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.inject</groupId>
            <artifactId>jersey-hk2</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <!-- uncomment this to get JSON support-->
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-binding</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>

    </dependencies>
    <properties>
        <jersey.version>2.26</jersey.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

UserResource.java

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

/**
 * @author rajdhagG
 *
 */

@Path("/AngularUser")
public class UserResource {

    UserRepository user = new UserRepository();
    User loginUser = new User();

    @POST
    @Consumes({MediaType.APPLICATION_JSON})
    @Produces({ MediaType.APPLICATION_JSON })
    @Path("login")
    public User searchUser(@QueryParam("uname") String uname, @QueryParam("paswd") String paswd){
        System.out.println("Logging in the with " + uname + ", " + paswd);
        System.out.println("This = > " + user.getUserDetails(uname, paswd));
        loginUser = user.getUserDetails(uname, paswd);
        if(loginUser.getUname() != "" && loginUser.getPaswd() != ""){
            return loginUser;
        }else {
            loginUser.setUname("Not Present");
            loginUser.setPaswd("Not present");
            return loginUser;
        }
    }
}

Response to Angular Service call with null username and password, event after passing it in the request

Restful Webservice IDE 控制台输出:

    Fri Apr 06 01:26:25 IST 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
    Logging in the with null, null
    This = > User [uname=, paswd=]

【问题讨论】:

    标签: java angular5 restful-architecture


    【解决方案1】:

    基本上,您创建了使用 application/json 内容的请求的服务方法,同时期望带有 application/x-www-form-urlencoded 内容的查询参数的请求。这两个是不匹配的。你必须直截了当,所以你有两个选择:

    • 将请求的内容类型更改为application/x-www-form-urlencoded,并通过删除@Consumes({MediaType.APPLICATION_JSON}) 注释来更改服务方法定义。
    • 更改服务方法以使用您自己的一些对象,自定义类Credentials,其中包含两个字段:用户名和密码。请求在其正文中包含序列化的 Credentials 对象。

    这两种解决方案中的一种是必要的,因此服务可以将您发送的请求与适当的服务方法相匹配。

    希望对你有帮助。

    【讨论】:

    • 感谢@bpawlowski的建议,我尝试了第一个选项,但实际上响应再次相同,而调试发现参数没有被打包到请求中的问题,并且得到错误为message:"params is not defined" stack:"ReferenceError: params is not defined↵ at eval (eval at AppGitService.postGitUser (webpack-internal 代码块 return this.proxy.post(this.url,{params:new HttpParams().set("uname","rajdharg").set("paswd","*****")}, {headers:headers}).subscribe(resp =&gt; { console.log("response %o , ", resp); });跨度>
    猜你喜欢
    • 2011-08-22
    • 2015-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-26
    • 1970-01-01
    相关资源
    最近更新 更多