【问题标题】:Spring boot REST aplication return HTML insted of JSON and Angular aplication not recognize itSpring Boot REST 应用程序返回 HTML 而不是 JSON 并且 Angular 应用程序无法识别它
【发布时间】:2020-05-12 13:12:09
【问题描述】:

我有一个由https://start.spring.io/ 生成的 Spring Boot 应用程序,但我无法让它生成 JSON 而不是 HTML,代码如下:

[@CrossOrigin(origins="http://localhost:4200",maxAge=3600)
@RestController
@RequestMapping
public class UsuariosController {
@Autowired
    UsuariosService ser;
    @GetMapping(path = "/entrar/{usu}/{pass}", produces = MediaType.APPLICATION_JSON_VALUE)
public Usuarios entrar(@PathVariable("usu") String usu,@PathVariable("pass") String pass){
    return ser.comprobarPass(usu, pass);
}

}

这是 Firefox 网络浏览器中的结果。1

这是 de comprobarPass 方法:

    @Override
public Usuarios comprobarPass(String usu, String pass) {
    return rep.compruebaUsuariopass(usu, pass);
}}

这是 POM.XML 的依赖部分:

    <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>2.10.1</version>
            </dependency>
 <dependency>

这是 Angular 代码:

    this.lo.verifyUserPass(this.usu,this.pass).subscribe((usua)=>{this.userloged=usua});
console.log("valor: "+this.userloged)

这是函数:

  verifyUserPass(usu:string,pass:string){
return this.http.get<Usuarios>(this.path+"entrar/"+usu+"/"+sha512.sha512(pass));

}

这是firefox中的输出enter image description here

【问题讨论】:

  • 结果丢失,但更重要的是您尝试访问应用程序的 URL! PS:将密码放入路径是一个安全问题!
  • 它正在生成 JSON ..你的问题是什么?
  • 好吧,看起来你提到的“HTML”只是用于显示 JSON 的 Firefox 内部渲染?使用 curl 重试验证。
  • 密码是SHA512加密
  • @PabloLleoGarcia 即使是散列密码也是一个问题,因为您的 API 似乎使用它来进行身份验证。顺便说一句:SHA512 是一种散列方法,不是加密,但这并不重要。而且您也绝不能在回复中返回密码!

标签: html json angular spring-boot


【解决方案1】:

这是因为你的返回类型不是一个可序列化的对象,你可以明确你的返回类型。

试试下面的代码

[@CrossOrigin(origins="http://localhost:4200",maxAge=3600)
@RestController
@RequestMapping
public class UsuariosController {
@Autowired
    UsuariosService ser;
    @GetMapping(path = "/entrar/{usu}/{pass}", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Usuarios> entrar(@PathVariable("usu") String usu,@PathVariable("pass") String pass){
    return ResponseEntity.ok().body(ser.comprobarPass(usu, pass));
}

还有你的可序列化的pojo

public class Usuarios implements serializable{
    //your pojo 
    }

【讨论】:

  • 这些都不需要!
  • 我无法导入静态 org.hibernate.type.TypeFactory.serializable;因为 TypeFactory 是删除线
【解决方案2】:

现在可以在 Angular 中使用此代码

this.lo.verifyUserPass(this.usu,this.pass).subscribe(usua=>{this.userloged=usua});

【讨论】:

    猜你喜欢
    • 2022-01-04
    • 2022-01-09
    • 2018-03-01
    • 2019-08-31
    • 2015-09-01
    • 2019-07-31
    • 2015-02-28
    • 2020-05-28
    • 2019-09-25
    相关资源
    最近更新 更多