Showing posts with label xml. Show all posts
Showing posts with label xml. Show all posts

13 December 2016

How to fix DefaultHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.web.HttpMediaTypeNotAcceptableException in Spring Boot ?

Solution works for : Spring Boot 1.4 and Spring 4.3.4 but it should works with older  and newer versions too.

STORY:

So you are learning Spring Boot and you learn quickly that in order to get  json response you simply need to write method in  Controller with @RestController annotation  (as shown on orange example in the end of this post).
However, if you try produce xml output instead (as shown on blue example) ,then ...unfortunately .. you will that Spring will display error:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Tue Oct 11 11:23:37 GMT 2016
There was an unexpected error (type=Not Acceptable, status=406).
Could not find acceptable representation

And in logs you will see:
2016-12-13 11:23:37.760  WARN 7456 --- [io-16801-exec-5] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation

If you see this error ,it means you forgot to add : jackson-dataformat-xml dependency to your project, so imply add this dependency to your gradle (or maven) .
For example for  Gradle:

compile('com.fasterxml.jackson.dataformat:jackson-dataformat-xml')

refresh your dependencies and re-run application and it should works :)

This an example:

@RestController
public class JsonController {

private static final Logger LOGGER = org.slf4j.LoggerFactory.getLogger(JsonController.class);

@GetMapping("/json")
public CuteDomainObject getCuteDomainObjectAsJson() {
CuteDomainObject cuteDomainObject = new CuteDomainObject("Name", new Random().nextInt(32));
LOGGER.info("Getting Cute Domain Object with data: " + cuteDomainObject.toString());
return cuteDomainObject;
}


@GetMapping(value = "/xml", produces = { "application/xml", "text/xml"})
public CuteDomainObject getCuteDomainObjectAsXml() {
CuteDomainObject cuteDomainObject = new CuteDomainObject("Name", new Random().nextInt(32));
LOGGER.info("Getting Cute Domain Object with data: " + cuteDomainObject.toString());
return cuteDomainObject;
}


}


Source: http://docs.spring.io/spring-boot/docs/current/reference/html/howto-spring-mvc.html#howto-write-an-xml-rest-service




13 January 2014

web development notes

WARNING!
This is personal notes for personal and my friends only:
http://dominiksymonowicz.blogspot.co.uk/2014/01/itnotes-information.html
You can read them,but I suggest look to link,resources only


It contains  all information about HTML,CSS and JavaScript (and their framework).



HTML

HTML notes:





CSS
CSS notes:
JavaScript
JavaScript notes:

  • Javascript scope
    • "The second important feature is that functions provide scope. In JavaScript there’s no  curly braces local scope; in other words, blocks don’t create scope. There’s only function scope. Any variable defined with var inside of a function is a local variable, invisible outside the function. Saying that curly braces don’t provide local scope means that if you define a variable with var inside of an if condition or inside of a for or a while loop, that doesn’t mean the variable is local to that if or for. It’s only local to the wrapping function, and if there’s no wrapping function, it becomes a global variable. minimizing the number of globals is a good habit, so functions are indispensable when it comes to keeping the variable scope under control." (find resource!)

JavaScript resources:
  • http://reference.sitepoint.com/javascript/domcore
XPath

Definition:
XPath is a language for finding information in an XML document.
XPath is used to navigate through elements and attributes in an XML document.


Basics:

  • XPath, there are seven kinds of nodes:
  1. attribute,
  2. comment,
  3. document nodes
  4. element,
  5. namespace,
  6. processing-instruction, 
  7. text 
  • XML documents are trees of nodes.
  • Items are atomic values or nodes.
  • Atomic values are nodes with no children or parent.
  • Each element and attribute has one parent. Element nodes may have zero, one or more children.
  • Nodes that have the same parent.
  • Ancestors A node's parent, parent's parent, Descendants A node's children, children's children,
  • XPath uses path expressions to select nodes or node-sets in an XML document.
  • Predicates are used to find a specific node or a node that contains a specific value.
  • XPath wildcards can be used to select unknown XML elements.
  • By using the | operator in an XPath expression you can select several paths.
  • An axis defines a node-set relative to the current node.
  • A location path can be absolute or relative.An absolute location path starts with a slash ( / ) and a relative location path does not.
  • An XPath expression returns either a node-set, a string, a Boolean, or a number.
  • XQuery is used to querying XML data.
  • XSLT is the style sheet language for XML files.

SOLUTIONS FOR COMMON PROBLEMS:

  • Note: In IE 5,6,7,8,9 first node is[0], but according to W3C, it is [1]. To solve this problem in IE, set the SelectionLanguage to XPath.
  •  How to prevent uneven linespacing  when subscripts or superscripts are used  on web pages.
Resources:


Misc. informations:
  • w3schools is quite famous website that provide web development learning content ,but there are many professionals who hats this page and they are even pages about it like http://www.w3fools.com .I am unable to jurge this,but it is worth to mention as it r

23 March 2013

How to insert % character into xml in android

As you know ,there are few illegal character to use in xml in Android.
  • &
  • <
  • >
  • "
  • '
However ,I was a bit suprised to see that .... using % character cause  a error too.To make things more weird .. using \% doesn't help either.

SOLUTION
Use:    \%%
to display %



11 March 2013

What are the difference between MATCH_PARENT and FILL_PARENT property in Android?

When you trying creating a layout for android,you can find that set  xml attributes for android:layout_height and android:layout_width contains 2 constants ( FILL_PARENT and  MATCH_PARENT  )and they have  same function . Confusing ? No worries,this post will help you to understand.

Let's start from basics. Some basics there are 3-4  types of xml attributes for android:layout_height and android:layout_width
  •  an exact number (you can using units like: px , dp , sp  , in or mm.  
  • WRAP_CONTENT, which means that the view wants to be just big enough to enclose its content (plus padding).  
  • FILL_PARENT,which means that the view wants to be as big as its parent (minus padding)  until API Level 8.  
  • MATCH_PARENT which means that the view wants to be as big as its parent (minus padding)  ...  in API Level 8 and higher.
As you discover that:
  1.  FILL_PARENT and   MATCH_PARENT has the same function!
  2. There is one difference.  MATCH_PARENT  is in use  since API level 8 and second one  FILL_PARENT  was used until API level 8,but you can still use,  this constant is deprecated.
So which value You should use?

SOLUTION:
this is my suggestions .
  1.  If you planning set  android:minSdkVersion to 7 and lower ,then use  FILL_PARENT. If you planning  set  android:minSdkVersion = 8 and higher  MATCH_PARENT. I  using  MATCH_PARENT  and i am not supporting Android 2.2 and lower anymore. 
  2. Universal solution is to use value : -1.As constant value of  FILL_PARENT and  MATCH_PARENT is -1. :)

See  official documentation for details:
http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html

BTW. Google renamed  FILL_PARENT  to   MATCH_PARENT  as  FILL_PARENT  was confusing for many developers.
Result?
There is even more confusion now as  readability of many examples is decreased and cause misunderstanding for beginners..



29 July 2011

How to add and configure log4j.XML file in Spring 3

Solution works for Spring 3.0.5 and log4j  1.2.16 (but it should works with previous/future releases of both programs)

It is really easy task:

I assumed that you already has spring 3 stuff in place.
  1. You need add log4j to your libraries in your project
  2. Add information about log4j to web.xml file (It should be /MyCozyProject/src/main/webapp/WEB-INF/web.xml  
  3. Add information about location for log4j.xml  in context.xml
  4. Configure your log4j 
Information which you need add in .2 (It tells spring where to look for information about log4j)
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

In 3. in context.xml (usually should be /src/main/webapp/META-INF/context.xml ) add information about where your log4j.xml file is
<Context displayName=" MyCozyProject ">
    <Parameter name="log4jConfigLocation" value="file:C:\myCozyProject\log4j.xml"/>
</Context>

In 4 You just configure your log4j to your needs (below is example of saving log to file)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

<appender name="tofile" class="org.apache.log4j.FileAppender">
  <param name="file" value="C:\\myCozyProject.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} %5p %c{1}:%L - %m%n"/>
</layout>
</appender>

<root>
<priority value ="info" />
<appender-ref ref="tofile" />
</root>

</log4j:configuration>


 btw. If you want log4j.xml inside project, then you can use my example or check this blog :
http://th1rty7.blogspot.com/2009/03/configure-logging-in-spring-with-log4j.html