Wednesday, September 27, 2017

View or Suppress Logback Status

Logback always shows status statements if there are configuration errors


Logback will log its own status statements to the console if you have any configuration problems resulting in WARN or ERROR messages, which will also show INFO messages. The Logback levels are, from lowest level to highest level:

TRACE < DEBUG < INFO < WARN < ERROR

(See Logback Architecture: error levels for details.)

See Logback status statements


To see Logback's status statements in the console (standard out) regardless of whether or not there are any configuration errors, set the debug attribute to true in the configuration element.
  <configuration debug="true">
    ...
  <configuration>

Suppress Logback status statements


To suppress display of Logback's status statements to the console, omit the debug attribute in the configuration element or set it to false. This will not suppress status statements if there are configuration problems with Logback.
  <configuration>
    ...
  </configuration>
If there is no debug attribute or it's set to false and you see Logback status statements, fix the problems causing the WARN or ERROR statements and the statements will no longer display.

If you can't fix the configuration problem but want to suppress the status statements, you can configure an alternate StatusListener like NopStatusListener to completely remove status statements.
  <configuration>
    <statusListener class="ch.qos.logback.core.status.NopStatusListener" />
    ...
  </configuration>
See also:

Logback Configuration: Automatic printing of status messages in case of warning or errors
Logback Configuration: Show status data
Logback Configuration

Wednesday, March 1, 2017

Use swagger-codegen to generate Java code from Swagger Spec

Swagger is the most popular standard for describing RESTful APIs. The Swagger Specification (formerly known as the OpenAPI Specification) is a JSON or YAML document that describes an API's endpoints, HTTP methods (GET, PUT, POST, etc.), possible responses and fields, possible errors, etc.

Swagger has code generators that will read a Swagger Specification and generate code in a variety of programming languages to consume the API. This saves a lot of time. The command line code generator is a Java Jar file. You need Java 7 or higher installed to run it. There is also a Maven plugin for Java projects that can generate the code for you as part of your project build process.


You can get help from the command line utility by running:
java -jar swagger-codegen-cli.jar help

which shows:
The most commonly used swagger-codegen-cli commands are:
    config-help   Config help for chosen lang
    generate      Generate code with chosen lang
    help          Display help information
    langs         Shows available langs
    meta          MetaGenerator. Generator for creating a new template set...
    version       Show version information

See 'swagger-codegen-cli help <command>' for more information on a specific command.

If you want detailed help about the generate command:
java -jar swagger-codegen-cli.jar help generate

The code generator can use a local Swagger doc or one hosted on a website. There is a quirk about how it handles URLs.

If the Swagger doc path is a URL and the URL has query parameters in it separated by an ampersand (&), you have to modify the URL slightly depending on whether you are using the command-line utility or the Maven plugin.

Command Line Utility (CLI)


When using swagger-codegen-cli.jar, enclose the -i (or --input-spec) parameter in double quotes if the URL has query parameters, like this:
-i "https://my.api.com/store/api-docs?provider=USER/me&name=MyNewService&version=v1"

For example:
java -jar swagger-codegen-cli.jar generate -l java --library jersey1 -o my/dir --model-package my.model.svc -i "https://my.api.com/store/api-docs?provider=USER/me&name=MyNewService&version=v1"

Maven swagger-codegen-maven-plugin


When using the Maven plugin, substitute &amp; for the & character if the URL has query parameters, like this:
https://mysite.com?color=red&flavor=sweet

Right: ?color=red&amp;flavor=sweet
<configuration>
  <inputSpec>https://mysite.com/api?provider=USER/me&amp;name=MyGreatService&amp;version=v1</inputSpec>
</configuration>

Wrong: ?color=red&flavor=sweet
<configuration>
  <inputSpec>https://mysite.com/api?provider=USER/me&name=MyGreatService&version=v1</inputSpec>
</configuration>

pom.xml

<build>
  <plugins>
    <plugin>
      <groupId>io.swagger</groupId>
      <artifactId>swagger-codegen-maven-plugin</artifactId>
      <version>2.2.1</version>
      <executions>
        <execution>
          <goals>
            <goal>generate</goal>
          </goals>
          <configuration>
            <inputSpec>https://my.api.com/store/api-docs?provider=USER/me&name=MyGreatService&version=v1</inputSpec>
            <language>java</language>
            <configOptions>
              <dateLibrary>joda</dateLibrary>
            </configOptions>
            <library>jersey1</library>
            <modelPackage>my.model.svc</modelPackage>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

Monday, February 6, 2017

Pretty Print JSON and XML from Mac OS Command Line

JSON


Must have Python 2.6 or higher version installed.

From Clipboard


After copying unformatted JSON to clipboard (ctrl+c), run this:

pbpaste | python -m json.tool

From File


After saving unformatted JSON to a file called, for example, ugly.json, run this to write it to a file called pretty.json:

cat ugly.json | python -m json.tool > pretty.json

XML


From Clipboard


After copying unformatted XML to clipboard (ctrl+c), run this:

pbpaste | xmllint --format -

pbpaste | xmllint --format -

From File


After saving unformatted XML to a file called, for example, ugly.xml, run this to write it to a file called pretty.xml:

cat ugly.xml | xmllint --format -o pretty.xml -


Set Cmder (ConEmu) console emulator to open new tab in current directory with Bash shell

Windows is a truly bad operating system for almost everything except games. Unfortunately sometimes we have to use it for web development. I...