none
Web server using REST API and jersey library in java RRS feed

  • Question

  • Hi!

    I want to implement a small prime numbers web service Using the REST API and the server written using the Jersey library.The server holds a database of prime and non-prime numbers . Clients can send requests with a number to check if it is registered in either of the two classes. If not, the client calculates the primeness for the number and sends a request to the server to store it in the server’s records. I have tried with following code I have only implemented part 2. In my code I calculate the primeness each time in the client part.

     My question here is how to implement the first part in assignment That is how to  implement  rest service for checking the primeness of a given number and.  This code for non_prime, I have isPrime class, it is similar to non_prime, I think that is enough to help me.

    @Path("Primes")
    public class Non_Prime {
    	
    	static ArrayList<String> nonprimes = new ArrayList<String>();
    	
    	@GET
    	@Path("/nonprime/{clientInput}")
    	@Produces(MediaType.TEXT_XML)
    	public String nonPrimes(@PathParam("clientInput") String clientInput){
    		
    		String xmlCatalogue = "<NonPrimes>" + "\n";
    		boolean numberAlreadyExists = false;
    		
    		for(int i = 0; i < nonprimes.size(); i++){
    			
    			xmlCatalogue = xmlCatalogue + "<Number>";
    			
    			xmlCatalogue = xmlCatalogue + nonprimes.get(i) + "\n";
    			
    			if(clientInput.equals(nonprimes.get(i))) 
    				numberAlreadyExists = true;
    			
    			xmlCatalogue = xmlCatalogue + "</Number>" + "\n";
    			
    		}
    		
    		if(!numberAlreadyExists){
    			
    			nonprimes.add(clientInput);
    			
    			xmlCatalogue = xmlCatalogue + "<Number>";
    					
    			xmlCatalogue = xmlCatalogue + clientInput + "\n";
    			
    			xmlCatalogue = xmlCatalogue + "</Number>" + "\n";
    			
    		}
    		
    		xmlCatalogue = xmlCatalogue + "</NonPrimes>";
    		
    		return xmlCatalogue;	
    		
    	}
    	
    }
    public class UserInfoClient {
    
        private static Scanner input;
    
    	public static void main(String[] args) {
        	
            ClientConfig config = new ClientConfig();
            Client client = ClientBuilder.newClient(config);
            WebTarget target = client.target(getBaseURI());
        	
    		input = new Scanner(System.in);
    		
    		System.out.println("Enter number that you want to check for primeness, end with 0");
    		
    		while(true){
    
    			int primeOrNot = input.nextInt();
    			boolean isPrime = true;
    			
    			if(primeOrNot == 0)
    				break;
    			
    			//Determine if number is prime or not
    			
    			for(int i = 2; i <= primeOrNot/2; i++){
    				
    				if((primeOrNot%i) == 0){
    					
    					isPrime = false;
    					break;
    					
    				}
    				
    			}
    			
    			if(isPrime){
    				
    				System.out.println("The number is prime\n");
    				
    				//Number is a prime, check if it exists in xml catalogue
    				//If it doesn't exist, insert it in to the catalogue
    	 String xmlAnswer = target.path("prime").path(String.valueOf(primeOrNot)).request().accept(MediaType.TEXT_XML).get(String.class);
    		        
    		        System.out.println(xmlAnswer);
    				
    			}
    			else{
    				
    				System.out.println("Number is not prime\n");
    				
    				//Number is not a prime, check if it exists in xml catalogue
    				//If it doesn't exist, insert it in to the catalogue
    				String xmlAnswer =
    		                target.path("nonprime").path(String.valueOf(primeOrNot)).request().accept(MediaType.TEXT_XML).get(String.class);
    				
    				System.out.println(xmlAnswer);
    				
    				
    				
    			}
    			
    		}
    		
        }
        
        private static URI getBaseURI(){
        	
            return UriBuilder.fromUri("http://localhost:8080/webtest/rest/Primes").build();
            
        }
        
        
    }

    Thank you for hand!
    Tuesday, December 10, 2019 5:45 PM

Answers

  • sara87 --

    You have erroneously posted your Java question in a public user forum dedicated to Microsoft Project Server, an enterprise project management application.  I would recommend you repost your question in a more relevant user forum.  Hope this helps.


    Dale A. Howard [MVP]

    Wednesday, December 11, 2019 10:29 PM
    Moderator

All replies

  • sara87 --

    You have erroneously posted your Java question in a public user forum dedicated to Microsoft Project Server, an enterprise project management application.  I would recommend you repost your question in a more relevant user forum.  Hope this helps.


    Dale A. Howard [MVP]

    Wednesday, December 11, 2019 10:29 PM
    Moderator
  • Hi, Thank you for help, do you know a more relevant user forum?

    Wednesday, December 11, 2019 10:37 PM