Skip to content

Instantly share code, notes, and snippets.

@nazartm
nazartm / pom.xml
Created February 17, 2013 14:18
Generating Schema and Java artifacts using maven jaxb plugin
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>1.3.1</version>
<executions>
<execution>
<id>Generate Java</id>
<goals>
<goal>xjc</goal>
</goals>
@nazartm
nazartm / unmarshalling.java
Created February 17, 2013 14:23
Umarshalling XML using JAXB
//WordList is a @XmlRootElement annotated class.
JAXBContext jaxbContext = JAXBContext.newInstance(WordList.class);
//Setting a Schema to validate against.
Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(new SAXSource(new InputSource(getClass().getClassLoader().getResourceAsStream("schema.xsd"))));
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
unmarshaller.setSchema(schema);
File inputFile; //initialize input file
WordList wordList = (WordList) unmarshaller.unmarshal(inputFile);
@nazartm
nazartm / Marshalling.java
Last active December 13, 2015 20:39
Marshalling Java to XML using JAXB with a specific namespace.
//WordList is a @XmlRootElement annotated class.
JAXBContext jaxbContext = JAXBContext.newInstance(WordList.class);
//Schema to validate against.
Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(new SAXSource(new InputSource(getClass().getClassLoader().getResourceAsStream("schema.xsd"))));
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "http://mysite.com/schema/v1/ xsd/schema.xsd");
marshaller.setSchema(schema);
@nazartm
nazartm / binding.xml
Created February 22, 2013 16:09
JAXB binding file for prefixing and suffixing generated classes and putting them under a specific package.
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
version="2.1" jaxb:extensionBindingPrefixes="xjc">
<jaxb:bindings schemaLocation="../schema/dto.xsd" node="/xs:schema">
<jaxb:schemaBindings>
<jaxb:package name="com.annagurban.nazar.dto.v1"/>
<jaxb:nameXmlTransform>
@nazartm
nazartm / DefaultTicketService.java
Created March 12, 2013 18:49
Classes with Field injections.
package com.example.service;
import com.example.domain.Ticket;
import com.example.service.mapper.Mapper;
import com.example.service.remote.RemoteTicketService;
import com.example.service.remote.TicketDto;
import java.util.Collections;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Named;
@nazartm
nazartm / DefaultTicketServiceTest.java
Created March 12, 2013 18:57
Testing classes with field injections using Mockito 1.9.5
package com.example.service;
import com.example.domain.Ticket;
import com.example.service.mapper.Mapper;
import com.example.service.remote.RemoteTicketService;
import com.example.service.remote.TicketDto;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
import static org.junit.Assert.*;
@nazartm
nazartm / DefaultTicketServiceTest2.java
Created March 12, 2013 19:21
Unit testing, integration testing classes with field injections using Spring.
package com.example.service;
import com.example.domain.Ticket;
import java.util.List;
import javax.inject.Inject;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@nazartm
nazartm / BlogService.java
Created March 18, 2013 18:10
Business interface of a service
public interface BlogService {
List<MyPost> getPosts();
MyPost getPost(int id);
int addPost(MyPost post);
boolean updatePost(int id, MyPost post);
@nazartm
nazartm / BlogRestfulService.java
Created March 18, 2013 18:14
Restful service interface
@Path("/blog")
@Consumes("application/xml")
@Produces("application/xml")
public interface BlogRestfulService {
@GET
@Path("/posts")
List<Post> getPosts();
@GET
@Named
@Scope("request")
public class BlogServiceImpl implements BlogRestfulService {
@Inject
private BlogService service;
@Inject
private Mapper mapper;
@Override