1

所以在后端我有 dropwizard,我想联系我的 /user/register 路由..

代码:

@Path("/user/register")
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response register(UserRB userRB) {

    String salt = BCrypt.gensalt(16);
    Response response;
    User newUser = new User();
    newUser.setName(userRB.getName());
    newUser.setMonths(new ArrayList<>());
    newUser.setPassword(BCrypt.hashpw(userRB.getPassword(), salt));
    newUser.setSalt(salt);

    for (User user : Ebean.find(User.class).findList()) {
        if (user.getName().equals(newUser.getName())) {
            return  Response.status(Response.Status.BAD_REQUEST).build();
        }
    }

    Ebean.save(newUser);
    response = Response.ok(newUser).status(Response.Status.CREATED).build();
    return response;
}

使用 Angular 应用程序,我想使用以下代码发出发布请求:

import { Injectable } from '@angular/core';
import {Http, RequestOptions} from '@angular/http';
import {User} from './user';

import 'rxjs/add/operator/toPromise';

@Injectable()
export class RegisterService {

private headers = new Headers({ 'content-type': 'application/json' });
private options =  new RequestOptions(this.headers);

constructor(private http: Http) { }

private handleError(error: any): Promise<any> {
console.error('An error just happened: ', error);
return Promise.reject(error.message || error);
}

register(name: String, password: String): Promise<User> {

return this.http.post('http://localhost:8080/timelogger/user/register',
  JSON.stringify( { name: name, password: password } ), this.options)
  .toPromise()
  .then((response) => console.log(response))
  .catch(this.handleError);
}

}

我得到一个 415 http 代码作为响应.. 不支持的媒体类型.. 和这个..

key:"content-type"
value:"Content-Type"
4

0 回答 0