Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7652203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:42:26+00:00 2026-05-31T11:42:26+00:00

Hi I am having trouble I am trying to learn restful services.I created a

  • 0

Hi I am having trouble I am trying to learn restful services.I created a web service using jax-rs which is shown below

@Path("/users")
public class Welcome {
@POST
@Consumes("text/xml")
@Produces("text/xml") 
public Response welcome(String incomingXML){
return Response.status(200).entity("timestamp : " + incomingXML).build();
}
}

I use the following test client to test the service

public class TestService {
public static void main(String args[]) throws ParserConfigurationException, SAXException, IOException {
ClientConfig config = new DefaultClientConfig();
Client client=Client.create(config);
WebResource service=client.resource(getBaseURI());
String urlString = "http://localhost:8080/JaXRSDemo/rest/users";
URL url = new URL( urlString );
HttpURLConnection con = (HttpURLConnection) url.openConnection();

// set up url connection to get retrieve information back
con.setRequestMethod( "POST" );
con.setDoInput( true );

// stuff the Authorization request header
byte[] encodedPassword = ( userName + ":" + password ).getBytes();

con.setRequestProperty( "Authorization",encodedPassword.toString() );
Customer customer=new Customer();
customer.setName("noobstre");
customer.setPin(123455);

ClientResponse response=service.path("rest").path("users").type(MediaType.APPLICATION_XML).post(ClientResponse.class,customer);
System.out.println(" response " + response.getEntity(String.class));
}
private static URI getBaseURI() {
return UriBuilder.fromUri("http://localhost:8080/JaXRSDemo").build();
}
}

I want to use the password in the header at the server side and do a lookup with the database.The problem I am facing is how do I read the headers at the server.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T11:42:27+00:00Added an answer on May 31, 2026 at 11:42 am

    I solved it using Jersey Client

    //clientside

        ClientConfig config = new DefaultClientConfig();
        Client client = Client.create(config);
        final String userName = "admin";
        final String password = "admin";
        String cred = userName + ":" + password;
        WebResource service = client.resource(getBaseURI());
    
        Customer customer = new Customer();
        customer.setName("noob");
        customer.setPin(123455);
        ClientResponse response = service.path("rest").path("users")
                .accept(MediaType.APPLICATION_XML)
                .header("Authorization", cred)
                .post(ClientResponse.class, customer);
    
        System.out.println(" response " + response.getEntity(String.class));
    

    At the server side

    @Path("/users")
    public class Welcome {
    
    @POST
    @Consumes(MediaType.APPLICATION_XML)
    @Produces(MediaType.APPLICATION_XML)
    public Response welcome(String incomingXML, @Context HttpHeaders headers) {
    
        String s = headers.getRequestHeaders().getFirst("authorization");
    
        return Response.status(200).entity("timestamp : " + incomingXML + s)
                .build();
    }
    

    }

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to learn Razor/MVC3 and having a bit of trouble with this partial
i'm having trouble trying to define this function-like macro, which takes 4 vector magnitudes
I am trying to learn about combinators and I am having trouble understand the
I'm trying to learn JavaScript arrays, and I'm having trouble duplicating how I've been
Hello I'm 17 and trying to learn how to code. I am having trouble
I'm trying to learn python and I'm having trouble importing a module. I have
I'm trying to learn the currently accepted features of c++11 and I'm having trouble
I'm trying to learn how to use HTML5 geolocation and am having trouble getting
I'm trying to learn smlnj at the moment and am having trouble with a
I am trying to learn Backbone js, and am having trouble understanding the difference

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.