通过Spring Resttemplate创建Jira用户失败,出现500异常

时间:2018-11-06 12:02:15

标签: java spring jira resttemplate

我正在尝试在JIRA系统中创建(以及其他)用户。为此,选择使用Spring RestTemplate。当用户的get工作时,创建的atm不起作用。我只收到以下消息500:
“请求失败。我们不确定出了什么问题。请重试,如果问题仍然存在,请联系Jira支持。” 我究竟做错了什么?我或多或少地将注意力放在https://www.baeldung.com/rest-template上,这里更具体地讲是带有用于发布请求的Exchange API的部分。 这是我最相关的测试功能,总是会失败:

public static void mainToJira()
{

    JiraUser testuser= new JiraUser();
    testuser.setDisplayName("TestUser 0");
    testuser.setName("TestUser0");
    testuser.setEmailAddress("test@test.test");
    testuser.setPassword("Passwort123");

    RestTemplateFactory factory = new RestTemplateFactory();
    factory.setHost("https://OURJIRASYSTEM/rest/api/latest/user");
    factory.afterPropertiesSet();
    RestTemplate restTemplate = factory.getObject();
    restTemplate.getInterceptors()
            .add(new BasicAuthorizationInterceptor("admin@user.de", "AdminPW4411"));

    org.springframework.http.HttpEntity<JiraUser> payload = new HttpEntity<JiraUser>(testuser);
    ResponseEntity<JiraUser> result=null;
    try
    {result = restTemplate.exchange("https://OURJIRASYSTEM/rest/api/2/user", HttpMethod.POST, payload,
            JiraUser.class);
    System.out.println((result.getStatusCode()));}
    catch (Exception e)
    {System.out.println(e.getMessage());}   

}

以下是其余的重现问题的依赖项:(Spring的详细信息主要来自示例)

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
@JsonIgnoreProperties (ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class JiraUser  {
    private String key;
    private String accountId;
    private String name;
    private String emailAddress;
    private String displayName;
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    private boolean active;
    private String locale;
//  private ArrayList<JiraGroup> groups;
//  private ArrayList<JiraApplicationRole> roles;   
    private String password;

//  public JiraUser()
//  {
//      groups=new ArrayList<JiraGroup>();
//      roles=new ArrayList<JiraApplicationRole>();
//  }

    public String getAccountId() {
        return accountId;
    }
    public String getDisplayName() {
        return displayName;
    }
    public String getEmailAddress() {
        return emailAddress;
    }
    public String getKey() {
        return key;
    }
    public String getLocale() {
        return locale;
    }
    public String getName() {
        return name;
    }
    public boolean isActive() {
        return active;
    }
    public void setAccountId(String accountId) {
        this.accountId = accountId;
    }
    public void setActive(boolean active) {
        this.active = active;
    }
    public void setDisplayName(String displayName) {
        this.displayName = displayName;
    }
    public void setEmailAddress(String emailAddress) {
        this.emailAddress = emailAddress;
    }
    public void setKey(String key) {
        this.key = key;
    }
    public void setLocale(String locale) {
        this.locale = locale;
    }
    public void setName(String name) {
        this.name = name;
    }   
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }


    public String toString()
    {

        return displayName+ " is active: "+active;
    }
}

工厂:

import org.apache.http.HttpHost;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component
public class RestTemplateFactory
  implements FactoryBean<RestTemplate>, InitializingBean {

    private RestTemplate restTemplate;

    private String hostName;
    private int port;
    private String protocol = "http";


    public RestTemplate getObject() {
        return restTemplate;
    }
    public Class<RestTemplate> getObjectType() {
        return RestTemplate.class;
    }
    public boolean isSingleton() {
        return true;
    }    

    public void afterPropertiesSet() {
        HttpHost host = new HttpHost(hostName, port, protocol);
        restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactoryBasicAuth(host));
    }
    public String getHost() {
        return hostName;
    }
    public void setHost(String host) {
        this.hostName = host;
    }
    public int getPort() {
        return port;
    }
    public void setPort(int port) {
        this.port = port;
    }

}

BasicAuth:

import java.net.URI;

import org.apache.http.HttpHost;
import org.apache.http.client.AuthCache;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;

public class HttpComponentsClientHttpRequestFactoryBasicAuth 
extends HttpComponentsClientHttpRequestFactory {

  HttpHost host;

  public HttpComponentsClientHttpRequestFactoryBasicAuth(HttpHost host) {
      super();
      this.host = host;
  }

  protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) {
      return createHttpContext();
  }

  private HttpContext createHttpContext() {
      AuthCache authCache = new BasicAuthCache();

      BasicScheme basicAuth = new BasicScheme();
      authCache.put(host, basicAuth);

      BasicHttpContext localcontext = new BasicHttpContext();
      localcontext.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
      return localcontext;
  }
}

1 个答案:

答案 0 :(得分:0)

问题是我们的Jira中已经有一个用户的电子邮件地址(test@test.test)。更改为不同的Mailaddress(肯定不存在)是可行的。