Spring REST 3 and JacksonMapper for JSON - web-services

We are currently using spring web services that support application/xml and application/json that works fine. A new service requires returning of an XML string. The string must be kept as XML but I find that application/json is also serializing the contents of the string. Is there any way to prevent this ?
Edits:
//dto:
#XmlRootElement(name="mydata")
public class MyData {
private String someData;
#XmlElement(required = true)
private String content;
#XmlElement
public String getSomeData(){
return someData;
}
public String getContent() {
return content;
}
//setter and other fields...
}
From the above, the content field is an XML string in my class. I would like the content field to be returned but not serialized. Is this possible ?

If this string is just serialized object (just like JSON with #ResponseBody), than everything you need is jackson-jaxrs lib and marking return bean with javax.xml.bind.annotation.* annotations:
//in controller:
#RequestMapping
public #ResponseBody MyData getMyData(){
MyData md = new MyData();
//...
return md;
}
//dto:
#XmlRootElement(name="mydata")
public class MyData {
private String someData;
#XmlElement
public String getSomeData(){
return someData;
}
//setter and other fields...
}

If you want to return XML, then add this annotation to the service:
#RequestMapping(method = RequestMethod.POST, value = "xyz", headers = "Accept=application/xml")

Related

Wso2 XACML get custom attributes

I am using Wso2 IS 5.11.0, I have a requirement where I need to fetch the user attributes by calling web-service based.
Below is the sample attribute finder code:
public class CustomPIPAttributeFinder extends AbstractPIPAttributeFinder {
private static final String PIP_NAME = "CustomPIPAttributeFinder";
private static final Set<String> SUPPORTED_ATTRIBUTES;
private static final Log log = LogFactory.getLog(CustomPIPAttributeFinder.class);
static {
SUPPORTED_ATTRIBUTES = new HashSet<String>();
SUPPORTED_ATTRIBUTES.add(CustomPIPConstants.SAMPLE_ATTRIBUTE_ID);
SUPPORTED_ATTRIBUTES.add(CustomPIPConstants.SAMPLE_ATTRIBUTE_NAME);
SUPPORTED_ATTRIBUTES.add(CustomPIPConstants.SAMPLE_CATEGORY);
}
#Override
public Set<String> getAttributeValues(URI attributeType, URI attributeId, URI category, String issuer,
EvaluationCtx evaluationCtx) throws Exception {
//code
}
private String retrieveSampleName(String accessToken) {
String sampleName = null;
// TODO: Get the value of the sample name from the sampleID from the datasource
return sampleName;
}
/**
* Since we override the {#link #getAttributeValues(URI, URI, URI, String, EvaluationCtx)} this won't be called.
*/
#Override
public Set<String> getAttributeValues(String subject, String resource, String action, String environment,
String attributeId, String issuer) throws Exception {
throw new UnsupportedOperationException("Method unsupported in the context");
}
public void init(Properties properties) throws Exception {
}
public String getModuleName() {
return PIP_NAME;
}
public Set<String> getSupportedAttributes() {
return SUPPORTED_ATTRIBUTES;
}
}
In the sample code we can fetch only one attribute per request.But how can we return multiple attributes before executing policy or get multiple attributes in one request from custom attribute finder. Is there any way to achieve this flow.
As per above code request attribute find(returns only one) in the example it will increase the overhead as for each attribute lookup we are calling web-service every time as it'll increase overhead.

Update User defined object of the dynamoDb item using UpdateItemRequest

We have a DynamoDB table Test which has an attribute Template. Bellow are the class definitions. I would like to update the Template attribute or some of its attribute based on certain condition. I tried doing the same using UpdateItemRequest but unable to find a way to update the template attribute since everything is converted to either string, number or bytes.
Code for reference.
#DynamoDbBean
public class Test implements NoSQLEntity {
private String name;
private Template template;
#DynamoDbAttribute("name")
public String getName() {
return name;
}
#DynamoDbAttribute("template")
public Template getTemplate() {
return template;
}
}
#DynamoDbBean
public class Template {
private String pk;
private String name;
private List<String> demo;
#DynamoDbAttribute("pk")
public String getPk() {
return this.pk;
}
#DynamoDbAttribute("name")
public String getName() {
return name;
}
#DynamoDbAttribute("demo")
public List<String> getdemo() {
return demo;
}
}
Sample update code:
UpdateItemRequest request = UpdateItemRequest.builder()
.tableName("Test")
.key(itemKey)
.updateExpression("SET tmpt = :tmt")
.expressionAttributeValues(expressionValues)
.build();
Here I am unable to build the :tmt using the AttributeValue. Can someone please guide me?
Here is a Readme from the Eng team that you may find useful:
https://github.com/aws/aws-sdk-java-v2/tree/master/services-custom/dynamodb-enhanced
DynamoDb Enhanced provides DefaultAttributeConverterProvider that will identify a converter that can convert the specific Java type into the AttrbuteValue type that can used to update the item.
Smaple Code:
AttributeValue value = DefaultAttributeConverterProvider.create()
.converterFor(EnhancedType.documentOf(Template.class,
TableSchema.fromBean(Template.class)))
.transformFrom(template);
Can then put this value in the expressionValues map.
Map<String, AttributeValue> expressionValues = new HashMap<>();
expressionValues.put(":tmt", value);
UpdateItemRequest request = UpdateItemRequest.builder()
.tableName("Test")
.key(itemKey)
.updateExpression("SET tmpt = :tmt")
.expressionAttributeValues(expressionValues)
.build();

#RequestParam spring MVC , make one of two request params mandatory

I am writing a service wherein I take in either an id or a location and I want to enforce the constraint that either the id or the location must be specified in my #Controller
#Controller
public class HelloController {
#RequestMapping(value="/loc.json",method = RequestMethod.GET)
public #ResponseBody String localiaztionRequest(#RequestParam(value = "location", required = false) String callback
,#RequestParam(value = "id", required = false) String uuid
,#RequestParam(value = "callback", required = false) String callback) {
//model.addAttribute("message", "Hello world!");
return "hello";
}
For clarity, I want each request to send either the location parameter or the id parameter. How do I enforce such a constraint on a pair of input parameters? Also as an aside could someone please explain to me the use of ModelMap , what is the effect of model.addAttribute("message","Hello World!") ?
Sorry if the questions seem rather naive, I'm extremely new to the spring framework.
Thanks in advance.
I think you should split it into two different controller methods
#RequestMapping(value="/loc.json",method = RequestMethod.GET, params={"location"})
public #ResponseBody String localizationRequestByLoc(#RequestParam String location, #RequestParam String callback) {
//model.addAttribute("message", "Hello world!");
return "hello";
}
#RequestMapping(value="/loc.json",method = RequestMethod.GET, params={"id"})
public #ResponseBody String localizationRequestById(#RequestParam String id, #RequestParam String callback) {
//model.addAttribute("message", "Hello world!");
return "hello";
}
One way to accomplish this (although indirect) would be to create a new object model representing the data to be passed in and another representing the data type and requiring both elements.
public enum MethodType
{
First,
Second
}
public class Filter
{
[Required]
public MethodType {get;set;}
[Required]
public string Data {get;set;}
}
use this object for the action method and view
and let the action method in the controller convert the object

How to send a property as a string in RestEasy response?

I have a class called Product which has a property called id of type long. Below is the class
public class Product {
private long id;
}
The value of id is beyond the value which javascript can handle. I realized this after seeing the below link
Parse json in javascript - long numbers get rounded
I dont want to declare the field as String in the domain class. But I want to say to RestEasy that it has to send the value as a string in the json response.
How can I do this? I dont want to use any third party api. Is it possible in RestEasy. I have gone through the documentation but did not find any such annotation or may be I did not go through the documentation properly.
Can anyone please help. Thanks all in advance.
If you are using Jackson as JSON Serializer you can extend the JacksonJsonProvider:
#Provider
public class JsonProvider extends org.codehaus.jackson.JacksonJsonProvider {
public JsonProvider() {
ObjectMapper objectMapper = locateMapper(ObjectMapper.class, MediaType.APPLICATION_JSON_TYPE);
objectMapper.configure(org.codehaus.jackson.JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS, true);
}
}
If you are using Jettison you can register a custom XmlAdapter:
public class LongAdapter extends XmlAdapter<String, Long> {
#Override
public String marshal(Long id) throws Exception {
if (id == null) {
return "";
}
return id.toString();
}
#Override
public Long unmarshal(String id) throws Exception {
return Long.parseLong(id);
}
}

Http 204 error in REST web service (Jersey)

I am using Jersey/Java to develop my REST services. I need to return an XML representation for my CarStore :
#XmlRootElement
public class CarStore {
private List<Car> cars;
public List<Car> getCars() {
return cars;
}
public void setCars(List<Car> cars) {
this.cars = cars;
}
Here is my Car object :
#XmlRootElement
> public class Car {
private String carName;
private Specs carSpecs;
private Category carCategory;
public String getCarName() {
return carName;
}
public void setCarName(String carName) {
this.carName = carName;
}
public Specs getCarSpecs() {
return carSpecs;
}
public void setCarSpecs(Specs carSpecs) {
this.carSpecs = carSpecs;
}
public Category getCarCategory() {
return carCategory;
}
public void setCarCategory(Category carCategory) {
this.carCategory = carCategory;
}
}
Specs and Category are enums like this :
#XmlRootElement
> public enum Category {
SEDANS, COMPACTS, WAGONS, HATCH_HYBRIDS, SUVS, CONVERTIBLES, COMPARABLE;
}
My resource class is :
#GET
#Produces({MediaType.APPLICATION_XML})
public CarStore getCars()
{
return CarStoreModel.instance.getAllCars();
}
My jersey client is :
WebResource service = client.resource(getBaseURI());
System.out.println(service.path("rest").path("cars").accept(
MediaType.APPLICATION_XML).get(String.class));
I am getting Http 204 error on access alongwith client exception :
com.sun.jersey.api.client.UniformInterfaceException
Any ideas ? Thanks !
EDIT : I have yet not developed the model class...I just initialized some car objects as dummy data and put them in carstore. Showing all the classes here would be very clumsy.
BTW, sorry for writing 204 Error..it is just that I am getting an Exception that led me think so.
I'm guessing the exception is not related to the response code (204) because 204 is a success condition that indicates "No Content."
I believe you are getting a UniformInterfaceException because your getCars() function is not returning an HTTP response body. The root problem is that your Car List isn't being converted into XML by JAXB because it is missing the #XmlElement annotation.
Your getCars() function should be:
#GET
#Produces(MediaType.APPLICATION_XML)
public CarStore getCars() {
// myCarStore is an instance of CarStore
return myCarStore.getCars();
}
and your Car List in CarStore should be defined:
#XmlElement(name="car")
private List<Car> cars;
Is what you're returning in xml format? I'm not sure what getAllCars does but you can use something like Fiddler to help you view the traffic and see what is being returned to the client and whether its in proper format etc
In your client code, is the resource path correct? Make sure getBaseURI is returning a value.
Perhaps try:
Client client = new Client();
WebResource resource = client.resource(getBaseURI());
CarStore carStore = resource.path("/rest/cars").accept(MediaType.APPLICATION_XML).get(CarStore.class);
Aren't you missing a #Path annotation on your resource class?
#GET
#Path("cars")
#Produces({ MediaType.APPLICATION_XML })
public CarStore getCars() {
return CarStoreModel.instance.getAllCars();
}
Check if the URL at which your REST WS is mounted the one you expect by putting a breakpoint in your getCars() method (or putting a System.out.println) to make sure it actually gets called.
It seems there is a hard coded check in Jersey to throw a UniformInterfaceException when a HTTP 204 is returned.
The best solution will be to 'fix' the rest server so it never returns a null. e.g. return an Empty list or a Class with not values set.
Else you will need to catch UniformInterfaceException which is really ugly
if (getStatus() == 204) {
throw new UniformInterfaceException(this);
}
More info here :
http://grepcode.com/file/repo1.maven.org/maven2/com.sun.jersey/jersey-client/1.17.1/com/sun/jersey/api/client/ClientResponse.java#ClientResponse.getEntity%28java.lang.Class%2Cjava.lang.reflect.Type%29