Skip to Main Content

Integration

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

deploy rest web services on weblogic

francy77Jul 20 2021
Hi all,
I'm new with weblogic and EJB v3;
I've published a RestFul webservices using EJB3:
package rest;

import javax.ejb.Stateless;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

@Stateless
@Path(value="/fattura")
public class FatturaServiceBean {

  @GET
  @Produces(value="text/plain")
  public String generaProssimoNumero(){
  	return "1234";
  }
   
}

As you can see very very easy and it works, but i can call it

http://localhost:7001/soap-web-service-ejb/resources/fattura

Now I'm asking why weblogic has added the word resources in the URI, tomEE dosn't add the word resources in the URI!!!! Is that a feature I can customize?
And also is there a way to tell weblogic to print in the log every web services is installed on it when it startup?
thanks
really much

Comments

mNem
Answer

Probably you want to build your body with a StringBuilder and then pass that to the sendMail() method.

Something like this ...

import java.io.IOException; 

    import java.nio.file.FileStore; 

    import java.nio.file.FileSystems; 

    import java.util.*; 

    import javax.mail.*; 

    import javax.mail.internet.*; 

    import javax.activation.*; 

     

    public class LinuxMounts { 

        public static void main(String[] args) throws IOException { 

             

            String mailFrom = "java@email"; 

            String mailTo = "<recipients@email>"; 

            String mailSubject = "Testing from Java"; 

            String mailBody = "You passed!"; 

            String mailHost = "localhost"; 

     

            StringBuilder sbMailBody = new StringBuilder();

            sbMailBody.append(printHeading()); 

            sbMailBody.append(mailBody);

     

     

            for (FileStore store : FileSystems.getDefault().getFileStores()) { 

                String strPath = store.toString(); 

     

                if (strPath.matches("^/(u01|backups).*$")){ 

                    long total = store.getTotalSpace() / 1024 / 1024 / 1024; 

                    long used = (store.getTotalSpace() - store.getUnallocatedSpace()) / 1024 / 1024 / 1024; 

                    long avail = store.getUsableSpace() / 1024 / 1024 / 1024; 

                    float percentage = total == 0 ? 0 :((used * 100) / total);  //prevent divide by zero exp

                    sbMailBody.append(String.format("%-50s %20d %20d %20d %20.2f %n", store, total, used, avail, percentage)); 

                } 

            } 

           

            System.out.println(sbMailBody.toString());

             

            sendEmail(mailFrom, mailTo, mailSubject, sbMailBody.toString(), mailHost); 

        } 

         

        static String printHeading(){ 

             

            String heading1 = "Filesystem"; 

            String heading2 = "Total Size (MB)"; 

            String heading3 = "Used Space (MB)"; 

            String heading4 = "Available Space (MB)"; 

            String heading5 = "Percentage Used (%)"; 

     

            String strHeading = String.format("%-50s %20s %20s %20s %20s %n", heading1, heading2, heading3, heading4, heading5); 

            return strHeading;

        } 

         

        public static void sendEmail(String fromEmail, String toEmail, String subject, String body,String mailHost){ 

     

            Properties properties = System.getProperties(); 

     

     

            //Setup mail server 

            properties.setProperty("mail.smtp.host", mailHost); 

     

     

            Session session = Session.getDefaultInstance(properties); 

     

     

            try { 

                MimeMessage message = new MimeMessage(session); 

     

     

                message.setFrom(new InternetAddress(fromEmail)); 

                message.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmail)); 

                message.setSubject(subject); 

                message.setText(body); 

     

                // Send message 

                Transport.send(message); 

                System.out.println("Sent message successfully...."); 

     

     

            } catch (MessagingException mex) { 

                    mex.printStackTrace(); 

              } 

        }  

    } 

Marked as Answer by frank.anellia · Sep 27 2020
unknown-7404
  1. publicstaticvoidsendEmail(StringfromEmail,StringtoEmail,Stringsubject,Stringbody,StringmailHost){

The program works. I would like to send the output of printHeading() and the mount values in the body of the email. Eventually, I would like to add the server name and some other details to the 'Subject' line.

Any ideas how I can pass those values to the Body and Subject of the email?

Not sure what you are asking.

See the above quote from you?

See those 'Stringsubject' and 'Stringbody' parameters?

Whatever you have in their is what will get used.

So if you want headings, mount values, or anything else you need to add incorporate them into those two strings.

Are you asking how to construct strings by concatenating other strings together?

The Java Tutorials has dozens of trails of how to use strings and the StringBuilder class.

https://docs.oracle.com/javase/tutorial/java/data/strings.html

frank.anellia

That worked!  I actually understand the coding to construct the email body.  I am receiving a "java.io.PrintStream' message in the email body:

  1. java.io.PrintStream@3957f3a4java.io.PrintStream@3957f3a4

...instead of the mounts output.  The header is there, just not the mounts.

Are you able to explain what you're doing with the following expression:

float percentage = total == 0 ? 0 : ((used * 100) / total);  //prevent divide by 0 exp

I know the error that you're avoiding by writing this but not sure how you came about it?

Thanks for your help, again!

mNem

Are you able to explain what you're doing with the following expression:

float percentage = total == 0 ? 0 : ((used * 100) / total);  //prevent divide by 0 exp 

I know the error that you're avoiding by writing this but not sure how you came about it?

You can think of it as the short form of ...

       
       float percentage = 0;
       if (total == 0) {
          percentage = 0;
       } else {
          percentage = ((used * 100) / total);
       }
      

On my system, it produced the error. In any case, it is good to make sure the total is not zero when it is used as a denominator.

As for the

I am receiving a "java.io.PrintStream' message in the email body:

what does it print to the console before invoking the sendMail() method?

            System.out.println(sbMailBody.toString());

             

            sendEmail(mailFrom, mailTo, mailSubject, sbMailBody.toString(), mailHost); 

I've been struggling to post this reply for about last half an hour. The reply box comes up with the spinner going forever.

mNem

Explanation from the official documentation about ternary conditional operator

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html

Another conditional operator is ?:, which can be thought of as shorthand for an if-then-else statement (discussed in the Control Flow Statements section of this lesson). This operator is also known as the ternary operator because it uses three operands. In the following example, this operator should be read as: "If someCondition is true, assign the value of value1 to result. Otherwise, assign the value of value2 to result."

The following program, ConditionalDemo2, tests the ?: operator:

class ConditionalDemo2 {

    public static void main(String[] args){

        int value1 = 1;

        int value2 = 2;

        int result;

        boolean someCondition = true;

        result = someCondition ? value1 : value2;

        System.out.println(result);

    }

}

Because someCondition is true, this program prints "1" to the screen. Use the ?: operator instead of an if-then-else statement if it makes your code more readable; for example, when the expressions are compact and without side-effects (such as assignments).

mNem

If you haven't sorted it out yet, check this one please...

       //sbMailBody.append(System.out.format("%-50s %20d %20d %20d %20.2f %n", store, total, used, avail, percentage));
       sbMailBody.append(String.format("%-50s %20d %20d %20d %20.2f %n", store, total, used, avail, percentage));
frank.anellia

That was it!  Your help is greatly appreciated!  Thanks!  Also, many thanks for explaining the conditional operators.

1 - 7

Post Details

Added on Jul 20 2021
0 comments
110 views